以下是应该返回相同文档的四个语句,即具有 id52dfc13ec20900c2093155cf
和 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)
前两个调用find
按预期工作,即它们返回由给定 id 标识的文档。第三次调用find
也可以工作,并返回与前面调用相同的文档。问题是最后一次调用......我希望它返回一个包含一个文档的列表,但事实并非如此。它只返回一个空列表。我错过了什么吗?