For example
> db.test.insert( { 'a':5 } )
> db.test.insert( { 'a': [5] } )
> db.test.find({ 'a':5})
{ "_id" : ObjectId("53e2b4366c9ef5cceb327e01"), "a" : 5 }
{ "_id" : ObjectId("53e2b43b6c9ef5cceb327e02"), "a" : [ 5 ] }
But, I only want to be able to match the first document.
The simplest way is to test for the presence of an array element using $exists and "dot notation":
db.test.find({ "a": 5, "a.0": { "$exists": false } })
Says find "a" equal to 5, but the first array element in "a" cannot exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With