Can someone tell me the difference between these two queries?
db.foo.find({ $and: [{a: 1}, {a: {$gt: 5}}]})
and
db.foo.find({a:1, a:{$gt:5}})
EDIT
Ok let me change the question a little bit. Assume the following
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
dev(mongod-2.2.0)>db.foo.insert({UserID: 1, Status:'unread' })
And I want to find all unread messages for user id 1. Do I do this
db.foo.find({UserID:1, Status:'unread'})
or this
db.foo.find({$and: [{UserID:1},{Status:'unread']})
With
$andboth conditions are taken into account when querying. Without$andonly the last spec forais considered in the query. This seems to happen with insert from mongo shell also.In your edited question, I would definitely use
db.foo.find({UserID:1, Status:'unread'})Example below: