Hide
Consider the following two documents:
In the first one, arr is an array with two entries. In the second, arr is just an object containing subdocuments. As the query shows, the ambiguity of "0" results in both documents matching.
Creating a text index on 'arr.0.str' yields only the embedded document as a matching result:
Removing the positional aspect of the field in the index definition results in the array document being returned as expected:
Show
Consider the following two documents:
> db.c.find({'arr.0.str':'abc'}).pretty()
{
"_id" : 1,
"arr" : [
{
"num" : 123,
"str" : "abc"
},
{
"num" : 789,
"str" : "xyz"
}
]
}
{
"_id" : 3,
"arr" : {
"0" : {
"num" : 123,
"str" : "abc"
},
"1" : {
"num" : 789,
"str" : "xyz"
}
}
}
In the first one, arr is an array with two entries. In the second, arr is just an object containing subdocuments. As the query shows, the ambiguity of "0" results in both documents matching.
Creating a text index on 'arr.0.str' yields only the embedded document as a matching result:
> db.c.createIndex({'arr.0.str':'text'})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.c.find({$text:{$search:'abc'}}).pretty()
{
"_id" : 3,
"arr" : {
"0" : {
"num" : 123,
"str" : "abc"
},
"1" : {
"num" : 789,
"str" : "xyz"
}
}
}
>
Removing the positional aspect of the field in the index definition results in the array document being returned as expected:
> db.c.dropIndex("arr.0.str_text")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.c.createIndex({'arr.str':'text'})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.c.find({$text:{$search:'abc'}}).pretty()
...
{
"_id" : 1,
"arr" : [
{
"num" : 123,
"str" : "abc"
},
{
"num" : 789,
"str" : "xyz"
}
]
}
>