$and is a (relatively) new feature, released in MongoDB version 2.0.
It is not available in 1.8.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24and
In order for your query to work, you must upgrade.
If upgrading is not possible for you at this time, 'and' queries were
implicit in versions 1.8 and below.
> db.services.save({ "ServiceType" : "sms" , "ServiceHealthState" : "ok"} )
> db.services.save({ "ServiceType" : "jms" , "ServiceHealthState" : "critical"})
> db.services.find({"ServiceType":"sms", "ServiceHealthState":"ok"})
{ "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
"ServiceHealthState" : "ok" }
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.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%7B%7B%24not%7D%7D
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.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24ne
> db.services.find({"ServiceHealthState":{$ne:"ok"}})
{ "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms",
"ServiceHealthState" : "critical" }
Hopefully the above examples will do what you were hoping to
accomplish.
Good Luck!