Ниже приведены четыре оператора, которые должны возвращать один и тот же документ, то есть документ с идентификатором 52dfc13ec20900c2093155cf
и адресом электронной почты [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)
Первые два вызова find
работают, как и ожидалось, т. е. они возвращают документ, идентифицированный данным идентификатором. Третий вызов find
также работает и возвращает тот же документ, что и предыдущие вызовы. Проблема в последнем вызове... Я ожидал, что он вернет список, содержащий один документ, но это не так. Он просто возвращает пустой список. Я что-то упускаю?