Counting Query documents

272 views
Skip to first unread message

Therefore

unread,
Sep 3, 2012, 7:18:04 PM9/3/12
to mongod...@googlegroups.com
Windows 7 64 SP1
MongoDB 2.2.0-rc2
Boost 1.42
MS VS 2010 Ultimate
C++ driver

According to:

http://www.mongodb.org/pages/viewpage.action?pageId=21270051

this will count the number of documents in a query:

    unsigned long long n = c.count("mydb.users", QUERY("age:"<<GT<<30));

but I get the error:

 error C2664: 'mongo::DBClientWithCommands::count' : cannot convert parameter 2 from 'mongo::Query' to 'const mongo::BSONObj &'

Thoughts?

Mark

David Hows

unread,
Sep 4, 2012, 12:33:16 AM9/4/12
to mongod...@googlegroups.com
Hi Mark,

Try the following: 

unsigned long long n = c.count("mydb.users", BSON("age"<<GT<<30));

Cheers,

David

Therefore

unread,
Sep 4, 2012, 1:58:45 AM9/4/12
to mongod...@googlegroups.com
Thanks David, that did the trick. Was the page incorrect or has there been a chance in subsequent MongoDB releases? Is there a way to get a Query object's count?

Mark


Wes Freeman

unread,
Sep 4, 2012, 2:09:26 AM9/4/12
to mongod...@googlegroups.com
Looks like David updated the page.

Wes

On Tue, Sep 4, 2012 at 1:58 AM, Therefore <therefo...@gmail.com> wrote:
Thanks David, that did the trick. Was the page incorrect or has there been a chance in subsequent MongoDB releases? Is there a way to get a Query object's count?


Mark


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

David Hows

unread,
Sep 4, 2012, 3:50:22 AM9/4/12
to mongod...@googlegroups.com
Yes, i did update the page to correct the issue you found!

Mark - as to finding the count of a query - not sure what you mean? Are you looking to execute a query, have the result set, get the number of objects in the result set and then perform actions upon that set? 

If that is what your after have a look at the following.
This is what we had done earlier :
unsigned long long n = c.count("test.counter", BSON("_id"<<GT<<5));
std::cout << n << std::endl;

This is how to get the number of objects remaining in a batch, which when executed right after getting the result set will give you the number of elements remaining in the cursor.
auto_ptr<DBClientCursor> cursor = c.query("test.counter", QUERY("_id"<<GT<<5));
int cnt = cursor->objsLeftInBatch();
std::cout << cnt << std::endl;

Hope that helps,

David

Therefore

unread,
Sep 4, 2012, 12:35:19 PM9/4/12
to mongod...@googlegroups.com
Hi David,

Thanks for the direction.  I guess I am looking for the equivalent of:

nstudents = db.students.find({'address.state' : 'CA'}).count();

which, according to my understanding of the documentation, will, on the server side, calculate the count of the entire query vs. a total only of the current batch of results the client has received.

In particular, I have a Query object qu passed as a parameter. Then:

  auto_ptr<DBClientCursor> = c.query( dc,qu.sort(sortby) );

Is there a way to get a count of my Query qu from the server?

Where is my misunderstanding?

Thanks for your help,

Mark

David Hows

unread,
Sep 4, 2012, 7:02:44 PM9/4/12
to mongod...@googlegroups.com
Mark,

Can you please post the details of the "qu" variable?

Without knowing what it is i cannot be certain, but i'd suggest that you simply run the count against the query - without the sort (as sort doesnt effect the number of objects).

Something akin to 
unsigned long long n = c.count("mydb.users", BSON(qu));

or 

unsigned long long n = c.count("mydb.users", qu);

Cheers,

David

Therefore

unread,
Sep 4, 2012, 8:16:53 PM9/4/12
to mongod...@googlegroups.com
Hi David,

Here is a shortened version of the code -- I notated where it raises an error. In a previous version of the code, I passed the query as a BSONObj and had no problem getting a server side count of the entire query. But I want to pass the query as a Query object so that I can use its helper methods such as sort/hint, etc. I suspect this is a naive question but can I convert a Query object back to a BSONObj so I can have the best of both worlds? I'm slogging through the online API code and haven't figured out how or if I can.

#include "stdafx.h"

#include <iostream>
#include <ctime>
#include "client/dbclient.h"
#include <string>

using namespace mongo;

// Print all documents in collection that match query, defaulting to all.
void printQuery(DBClientConnection &c, std::string &dc, Query qu = BSONObj() ) {

    auto_ptr<DBClientCursor> cursor
= c.query(dc, qu);

    // The count of documents in batch but not the entire query
which isn't what I want
    int count = cursor->objsLeftInBatch();

    // The count of documents in collection
, which works fine.
    unsigned long long dcCount = c.count(dc);

    // Would like count of all documents in query on the server side, but the following raises the error

    // error C2664: 'mongo::DBClientWithCommands::count' :
    // cannot convert parameter 2 from 'mongo::Query' to 'const mongo::BSONObj &'

    unsigned long long quCount = c.count(dc, qu);

    // ... lots of other code
}

int main() {
    DBClientConnection c;
    try {
        c.connect("localhost");
        cout << "We are connected...."<<endl;
    }
    catch (DBException &e)
    {
        cout << "Error connecting. Reason: "<<e.what()<<endl;
        return 0 ;
    }
    std::string database("testdb");
    std::string collection("docs");
    std::string datacollection;
    datacollection = database + "." + collection;

    Query querySelector = QUERY( "attrs.pingtime" << LTE << 20 );
    printQuery(c, datacollection, querySelector );

    return 0;
}

Thanks for your help!

Mark

David Hows

unread,
Sep 4, 2012, 9:23:47 PM9/4/12
to mongod...@googlegroups.com
Hi Mark,

Forgive me if im misinterpreting you.

You can't readily convert from a Query object back to a BSON, but the reverse is doable.

Assuming a BSON object of qu which contains the basic query you can do something like:

BSONObj qd = BSON("_id"<<GT<<5);
Query qd = Query(qu);
auto_ptr<DBClientCursor> cursor = c.query(dc, qd.sort("_id", -1));

This means you would pass the BSON object around and convert it to a Query object as needed.

Cheers,

David

On Tuesday, September 4, 2012 9:18:05 AM UTC+10, Therefore wrote:

David Hows

unread,
Sep 4, 2012, 9:52:37 PM9/4/12
to mongod...@googlegroups.com
Sorry. Wrong BSONObj, should be.

BSONObj qu = BSON("_id"<<GT<<5);

Therefore

unread,
Sep 4, 2012, 10:47:52 PM9/4/12
to mongod...@googlegroups.com
Thanks David, that will do.

I'm working my way through an understanding of when and how to use the Query object. According to "MongoDB in Action": "Unique to C++ is the requirement that you explicitly mark BSON documents that will be used as query selectors." I took that to mean that you must use a Query object (the said explicitly marked BSON document) as a query selector. But I can skip the Query() constructor, use BSON and set it to a BSONObj, e.g

      BSONObj querySelector = BSON("attrs.pingtime" << LTE << 20);
      printQuery(c, datacollection, querySelector);

The printQuery function's 3rd parameter is a Query object but it accepts the BSONObj selector which now makes sense.

Or I can skip the Query() constructor, use BSON, and set it to a Query object.

       Query querySelector = BSON( "attrs.pingtime" << LTE << 20 );

And they both work as well as:

   Query querySelector = Query( BSON("attrs.pingtime" << LTE << 20) ) ;

or using the macro:


   Query querySelector = QUERY( "attrs.pingtime" << LTE << 20 );

A very naive question: Couldn't the helper methods of the Query class have been implemented in BSONObj? Or, conversely, why couldn't a server-side count method be implemented with Query?

Thanks,

Mark

David Hows

unread,
Sep 4, 2012, 11:56:57 PM9/4/12
to mongod...@googlegroups.com
Mark,

MongoDB has gone through a few releases since the publication of "MongoDB In Action" so this particular aspect of the C++ driver may have been changed since then.

I can't comment on the specific reasons as to why decisions were made in the driver, however if you have specific feedback for improving the driver feel free to raise a jira ticket at https://jira.mongodb.org or branch your own release of the C++ driver, make these changes and submit a pull request :)

Good Luck,

David

On Tuesday, September 4, 2012 9:18:05 AM UTC+10, Therefore wrote:

Therefore

unread,
Sep 15, 2012, 1:42:43 PM9/15/12
to mongod...@googlegroups.com
The answer to this came from "ForEveR" at:

http://stackoverflow.com/questions/12333725/mongodb-c-driver-server-side-query-count

You can get a server-side count by using the Query public member obj which is  BSONObj:

c.count("mydb.users", qu.obj);
Reply all
Reply to author
Forward
0 new messages