The Mongo documentation on the $not operator states that, "The $not
meta operator can be used to negate the check performed by a standard
operator." There are some examples in the documentation of how the
$not operator is used.
If you would like to return all all of the documents in which the key
"ServiceHealthState" is not equal to "ok", the "$ne" (not equal)
operator should be used.
On Thu, Nov 3, 2011 at 4:51 PM, Marc <m...@10gen.com> wrote: > $and is a (relatively) new feature, released in MongoDB version 2.0. > It is not available in 1.8.
> If you upgrade to the latest version of MongoDB, $and will work as > advertised: > > db.version() > 2.0.1 > > db.services.find() > { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms", > "ServiceHealthState" : "ok" } > { "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms", > "ServiceHealthState" : "critical" } > > db.services.find({$and:[{"ServiceType":"sms"}, > {"ServiceHealthState":"ok"}]}) > { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms", > "ServiceHealthState" : "ok" }
> The Mongo documentation on the $not operator states that, "The $not > meta operator can be used to negate the check performed by a standard > operator." There are some examples in the documentation of how the > $not operator is used.
> If you would like to return all all of the documents in which the key > "ServiceHealthState" is not equal to "ok", the "$ne" (not equal) > operator should be used.
> Hopefully the above examples will do what you were hoping to > accomplish.
> Good Luck!
> -- > You received this message because you are subscribed to the Google Groups > "mongodb-user" group. > To post to this group, send email to mongodb-user@googlegroups.com. > To unsubscribe from this group, send email to > mongodb-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/mongodb-user?hl=en.
>> If you upgrade to the latest version of MongoDB, $and will work as >> advertised: >> > db.version() >> 2.0.1 >> > db.services.find() >> { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms", >> "ServiceHealthState" : "ok" } >> { "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms", >> "ServiceHealthState" : "critical" } >> > db.services.find({$and:[{"ServiceType":"sms"}, >> > {"ServiceHealthState":"ok"}]}) >> { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms", >> "ServiceHealthState" : "ok" }
>> The Mongo documentation on the $not operator states that, "The $not >> meta operator can be used to negate the check performed by a standard >> operator." There are some examples in the documentation of how the >> $not operator is used.
>> If you would like to return all all of the documents in which the key >> "ServiceHealthState" is not equal to "ok", the "$ne" (not equal) >> operator should be used.
>> Hopefully the above examples will do what you were hoping to >> accomplish.
>> Good Luck!
>> -- >> You received this message because you are subscribed to the Google Groups >> "mongodb-user" group. >> To post to this group, send email to mongodb-user@googlegroups.com. >> To unsubscribe from this group, send email to >> mongodb-user+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/mongodb-user?hl=en.
> -- > Cheers, > Shiraz
> -- > You received this message because you are subscribed to the Google Groups > "mongodb-user" group. > To post to this group, send email to mongodb-user@googlegroups.com. > To unsubscribe from this group, send email to > mongodb-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/mongodb-user?hl=en.
Scott, it's obvious once you mentioned it. Filtering on multiple
fields does not require the use of $and, so it's fine in pre-2.0
MongoDB. I haven't used $and yet. I see why $and is needed with
multikeys. Are there cases where it's useful outside of multikeys?
Shiraz, I hope you don't mind: I used your idea in a sample report to
make sure that things worked the way I expected them to work. I posted
my report showing the multi-field filter here:http://
mdahlman.wordpress.com/2011/11/03/mongodb-multi-field-filter/
Regards,Matt
On Nov 3, 9:13 am, Scott Hernandez <scotthernan...@gmail.com> wrote:
> >> If you upgrade to the latest version of MongoDB, $and will work as
> >> advertised:
> >> > db.version()
> >> 2.0.1
> >> > db.services.find()
> >> { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
> >> "ServiceHealthState" : "ok" }
> >> { "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms",
> >> "ServiceHealthState" : "critical" }
> >> > db.services.find({$and:[{"ServiceType":"sms"},
> >> > {"ServiceHealthState":"ok"}]})
> >> { "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
> >> "ServiceHealthState" : "ok" }
> >> The Mongo documentation on the $not operator states that, "The $not
> >> meta operator can be used to negate the check performed by a standard
> >> operator." There are some examples in the documentation of how the
> >> $not operator is used.
> >> If you would like to return all all of the documents in which the key
> >> "ServiceHealthState" is not equal to "ok", the "$ne" (not equal)
> >> operator should be used.
> >> Hopefully the above examples will do what you were hoping to
> >> accomplish.
> >> Good Luck!
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "mongodb-user" group.
> >> To post to this group, send email to mongodb-user@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> mongodb-user+unsubscribe@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/mongodb-user?hl=en.
> > --
> > Cheers,
> > Shiraz
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongodb-user@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/mongodb-user?hl=en.
There are many cases when $and is useful. In a nutshell (from the
feature request) "Condition blocks are physically objects. Thus, we
cannot have the same property used twice, preventing the use of the
same construct/property multiple times in a query."
There are several examples of use cases for $and in the Jira
ticket: https://jira.mongodb.org/browse/SERVER-1089