Windows 7 64 SP1 —
MongoDB 2.2.0-rc2 —
Boost 1.42 —
MS VS 2010 Ultimate —
C++ driver
I have a function that takes a Query object as a parameter:
someFunction( Query qu )
Advantages:
- Can accept either a Query object or a well-formed BSONObj.
- Have access to Query helpers such as sort/hint/etc.
Disadvantage:
-
Can’t do a server-side count (vs. a client-side count of a batch of results)
akin to the shell’s:nstudents = db.students.find({'address.state' : 'CA'}).count();i.e.,
unsigned long long n = c.count("mydb.users", qu);raises the error:
cannot convert ... from 'mongo::Query' to 'const mongo::BSONObj &
So, it was suggested I use a BSONObj as a parameter:
someFunction ( BSONObj qu )
Advantages:
- Can do a server side count.
- Can convert to a Query and hence use its helpers.
Disadvantage:
- Anyone using the function must be aware not to pass a query
as a Query object which is counter-intuitive.
So, my questions are:
Why aren’t the helper methods of the Query class implemented in BSONObj? Or, conversely, why couldn’t a server-side count method be implemented with the Query class?
So,
countshould receiveBSONObj(or Base/Derived of/fromBSONObj).Queryhasimplicit c-tor, that receivesBSONObj.Queryhas public memberobj, that isBSONObj.Your function can be
and for call
countyou should use