0

Given the following document, how can I query for a document where meta does not contain an object with a specific name?

{ "_id" : 1, "meta" : [ { "name" : "alpha", "date" : ISODate("2015-09-08T19:51:03.275Z") } ] }

I'm looking for the opposite of this:

db.content.find({'meta.name': 'alpha'})

I want to query for the same document like this:

Find me the document where 'meta.name' !== 'beta'.
2
  • I can't get $in or $nin to work with this example. Commented Sep 9, 2015 at 1:38
  • 1
    Related: To $nin or not to $nin as you seem to misconceive that you need such operators when working with arrays as that post discusses. Commented Sep 9, 2015 at 2:05

2 Answers 2

3

Try

 db.content.find({'meta.name': {$ne: "beta"}})

since $ne works for nested arrays too

Sign up to request clarification or add additional context in comments.

Comments

0

Came through few such questions previously.Assuming your collection as 'test'. Try this instead

db.test.find({"_id":1, "meta":{$elemMatch :{"name":{$ne:"beta"}}}});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.