简体   繁体   中英

Query where array of object does not contain property equal to string value

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'.

Try

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

since $ne works for nested arrays too

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM