Question

Here below are four statements that should return the same document, i.e. the one with id 52dfc13ec20900c2093155cf and email me@gmail.com:

val collection = ReactiveMongoPlugin.db.collection[JSONCollection]("users")

collection.indexesManager.ensure(
  Index(List("email" -> IndexType.Ascending), unique = true)
) 

// Returns one document, i.e. the one identified by 52dfc13ec20900c2093155cf
collection.find(Json.obj("_id" -> Json.obj("$oid" -> "52dfc13ec20900c2093155cf"))).cursor[JsValue].headOption

// Returns a list containing one element, i.e. the one identified by 52dfc13ec20900c2093155cf
val query = collection.find(son.obj("_id" -> Json.obj("$oid" -> "52dfc13ec20900c2093155cf"))).options(QueryOpts(skipN = 1)).cursor[JsValue].collect[Vector](1)

// Returns the same document as above, but found by email instead of by id
collection.find(Json.obj("email" -> "me@gmail.com")).cursor[JsValue].headOption

// Returns an empty list (it should return the same document as above)
val query = collection.find(Json.obj("email" -> "me@gmail.com")).options(QueryOpts(skipN = 1)).cursor[JsValue].collect[Vector](1)

The first two calls to find work as expected, i.e. they return the document identified by the given id. The third call to find also works and returns the same document as the previous calls. The problem is the last call... I'd expect it returns a list containing one document, but it doesn't. It just returns an empty list. Am I missing something?

Was it helpful?

Solution

You are asking one record to be skipped as part of your query.

QueryOpts(skipN = 1))

I assume you did not intend to do this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top