1

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:

  1. Can accept either a Query object or a well-formed BSONObj.
  2. Have access to Query helpers such as sort/hint/etc.

Disadvantage:

  1. 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:

  1. Can do a server side count.
  2. Can convert to a Query and hence use its helpers.

Disadvantage:

  1. 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?

Therefore
  • 367
  • 2
  • 13

1 Answers1

1
unsigned long long count (const string &ns, const BSONObj &query=BSONObj(),
int options=0)

So, count should receive BSONObj (or Base/Derived of/from BSONObj).

Query has implicit c-tor, that receives BSONObj.

Query (const BSONObj &b)

Query has public member obj, that is BSONObj.

Your function can be

// i think const Query& qu will be better.
someFunction( Query qu )

and for call count you should use

c.count("mydb.users", qu.obj);
ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Thank you -- that clears up my misunderstanding. I wish I could vote you useful, but alas I'm under 15. – Therefore Sep 08 '12 at 21:02
  • @Therefeore: you can also accept an answer if it solves your question :). – Stennie Sep 09 '12 at 07:32
  • My function is `void printQuery(DBClientConnection &c, std::string &dc, const Query &qu = BSONObj(), std::string sortby = "" )`. But `cursor = c.query(dc,qu.sort(sortby))` raises the error: `error C2663: 'mongo::Query::sort' : 2 overloads have no legal conversion for 'this' pointer`. I believe this is because of the `const Query` using the member function `sort`. But if I change it to `Query&` without the `const`, then my parameter initialization `= BSONObj()` raises `cannot convert from 'mongo::BSONObj' to 'mongo::Query &'`. Is it really bad form to use a default value for a param? Thoughts? – Therefore Sep 11 '12 at 00:41
  • @Stennie or ForEveR Could one of you please consider adding [tag:mongodb-c++] tag to this question as I could not create it myself. [tag:mongodb-c] is misleading and I don't belive that the combination of [tag:mongodb] and [tag:c++] cuts it. There already exists [tag:mongodb-csharp], etc. But obviously, you are more aware of what entails tag pollution that I. – Therefore Sep 11 '12 at 03:03
  • @Therefore: I think [tag:mongodb-c] is a good example of why it would be better *not* to add another tag. There are currently only 9 questions tagged with [tag:mongodb-c], and several of these are C++. Askers may be missing out on potential answers from the much larger group of users following [tag:mongodb] and [tag:c++], unless they also double up on the tags. You can do a search with multiple tags for the equivalent results without the risk of tagging getting too specific eg: [mongodb c++](http://stackoverflow.com/questions/tagged/c%2b%2b%20mongodb). – Stennie Sep 11 '12 at 11:41
  • @Stennie Thanks for the advice -- I'll continue to use the mongodb & c++ tags -- it makes more sense by inviting a greater set of eyes on the questions. – Therefore Sep 11 '12 at 19:20