I am trying to select a document that does NOT contain a value in an array of documents.
I have two problems that I will cite separately:
(1) I cannot get the $ not operator to work with a value in an array: 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 checking the absence of orange:
db.myCol.find({$not:{mylist:"orange"}})
Why is this not working?
(2) I cannot get the value in the array request to work if the array values ββare ObjectIds:
{ _id: Object("000000000000000000000000"), mylist: [ ObjectId("111111111111111111111111") ] }
Below this document is NOT retrieved:
myCol.find({mylist:ObjectId("111111111111111111111111")})
Can anyone suggest what I may be doing wrong?
source share