以下は、同じドキュメント、つまり id52dfc13ec20900c2093155cf
と emailを持つドキュメントを返す必要がある 4 つのステートメント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)
最初の 2 つの呼び出しはfind
期待どおりに動作します。つまり、指定された ID で識別されるドキュメントを返します。への 3 番目の呼び出しfind
も機能し、前の呼び出しと同じドキュメントを返します。問題は最後の呼び出しです... 1 つのドキュメントを含むリストが返されることを期待していますが、そうではありません。空のリストを返すだけです。何か不足していますか?