Reputation: 461
I can only find examples to find if specific Keys do not exist.
I have specific documents in my MongoDB instance that have fields like this:
"actions" : [
{
"date" : "2015-03-09T15:28:03Z",
"reason" : "",
"begin_date" : "2015-03-09T15:28:03Z",
"end_date" : "2015-03-09T15:28:03Z",
"action_type" : "Block",
"performed_date" : "2015-03-09T15:28:03Z",
"active" : "on",
},
{
"date" : "2015-03-09T15:28:03Z",
"reason" : "",
"begin_date" : "2015-03-09T15:28:03Z",
"end_date" : "2015-03-09T15:28:03Z",
"action_type" : "Alert",
"performed_date" : "2015-03-09T15:28:03Z",
"active" : "on",
},
{
"date" : "2015-03-09T15:28:03Z",
"reason" : "None",
"begin_date" : "2015-03-09T15:28:03Z",
"end_date" : "2015-03-09T15:28:03Z",
"action_type" : "History",
"performed_date" : "2015-03-09T15:28:03Z",
"active" : "on",
}
],
Now, I am trying to generate a query to return all records where the "action_type": "History" does not exist.
I know the $exists operator can tell me if the "action_type" key doesn't exist, but I can't find any examples to tell me where I can specify what value doesn't exist.
In my head I want to say it's something like this (but obviously this doesn't work):
db.collection.find({ "actions":{"$elemMatch":{"action_type": "History"}} : { '$exists': false} } )
Hopefully this is a lot easier than I am making it out to be.
Upvotes: 3
Views: 2664
Reputation: 6237
This should work:
db.collection.find({"actions.action_type": {"$ne": "History"}})
Upvotes: 3
Reputation: 884
Are you essentially looking to check if a value is not something? i.e. the key will always exist but you want matches where the value is not "history".
If so, there's the Not Equals operator:
http://docs.mongodb.org/manual/reference/operator/query/ne/
Upvotes: 1