Reputation: 4171
I'm newbie in mongodb. I think there is some usage differences between mongo shell and php mongo driver.
I can count with multiple criteria in mongo shell. For example:
db.coll.count( { _id:ObjectId('4fb27b9c876b88e53d000000'),
'items.title':'example' } );
But I can't make the same counting with php mongo driver. I tried this but it returns 0.
$coll->count( array('_id'=>new MongoID('4fb27b9c876b88e53d000000'),
'items.title'=>'example' ) );
However, if I use find() and then count(), I get the same counting result:
$coll->find( array('_id'=>new MongoID('4fb27b9c876b88e53d000000'),
'items.title'=>'example' ) ).count();
I get what I want, but I think the chaining way might be inefficient. Am I wrong?
How can you count with multiple criteria?
Upvotes: 0
Views: 1217
Reputation: 8833
You must first use find() and than count()
db.foo.count() -- will return 0, because db.foo it is only accessing the collection
db.foo.find($where).count() --- will return the number of documents in the collection
This is how to do it.
Upvotes: 0
Reputation: 230396
One code sample is worth a thousand words. Look:
% mongo
MongoDB shell version: 2.1.1
connecting to: test
> db.foo.count
function (x) {
return this.find(x).count();
}
Did this answer your question? :)
Upvotes: 3