j3d
j3d

Reputation: 9724

ReactiveMongo: find() does not return anything when seeking by a field different that _id

Here below are four statements that should return the same document, i.e. the one with id 52dfc13ec20900c2093155cf and email [email protected]:

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" -> "[email protected]")).cursor[JsValue].headOption

// Returns an empty list (it should return the same document as above)
val query = collection.find(Json.obj("email" -> "[email protected]")).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?

Upvotes: 0

Views: 1021

Answers (1)

aks
aks

Reputation: 720

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?

Upvotes: 1

Related Questions