I am trying to select a document which does NOT contain a value in a document's array.
I'm having two problems, which I'll present separately:
(1) I cannot get the $not operator to work with a value-in-array query: For example, if I have the following document in my collection:
{ _id: ObjectId("000000000000000000000000"),
mylist: [ "red", "green", "blue" ] }
I can select this document using:
db.myCol.find({mylist:"red"})
However, I would like to select this document by testing for the absence of orange:
db.myCol.find({$not:{mylist:"orange"}})
Why does this not work?
(2) I cannot get the value in array query to work if the array values are ObjectIds:
{ _id: Object("000000000000000000000000"),
mylist: [ ObjectId("111111111111111111111111") ] }
The following does NOT retrieve this document:
myCol.find({mylist:ObjectId("111111111111111111111111")})
Can someone suggest what I might be doing wrong?
There is no $not
that works like, you want $ne
for simple cases like yours:
db.myCol.find({ mylist: { $ne: 'orange' } })
Your second should (and does) work fine for me, perhaps you're using the wrong number of ones.
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