0

Windows 7 64 SP1 -- MongoDB 2.2.0-rc2 -- Boost 1.42 -- MS VS 2010 Ultimate -- C++ driver

I've written this function:

void printQuery(DBClientConnection &c, std::string &dc, const Query &qu = BSONObj(), std::string sortby = "" )

This fragment:

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

raises the error:

error C2663: 'mongo::Query::sort' : 2 overloads have no legal conversion for 'this' pointer.

sort (const string &field, int asc=1) should be the applicable overload. I believe this is something to do with using const Query& with its 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 &'

If I pass by value, then it compiles fine.

Is there a way to avoid either of the errors (beside passing by value)? Thanks!

Therefore
  • 367
  • 2
  • 13
  • Why you use default value for query? – ForEveR Sep 12 '12 at 07:00
  • Because I want to be able to call it without a Query so that it will print all documents in the collection by default. I did rewrite using overloaded functions but came across the same issue. – Therefore Sep 13 '12 at 00:08
  • I rewrote using overloaded functions without the `const` but came across a similar issue -- I could no longer pass BSONObjs --`mongo::BSONObj` could not be converted to `mongo::Query &` All of the Query objects passed just fine. – Therefore Sep 13 '12 at 00:24

2 Answers2

1

David Hows at MongoDB-user walked me through the solution:

Instead of const Query &qu = BSONObj(), use Query &qu = Query().

  1. I was getting an error using const "because sort will change the value of the query object - which is defined as a constant." So I dropped it.

  2. Using BSONObj() as the default is problematic because I'm not "creating a new object but assigning a new BSONObj into a variable for a Query object, nothing new is being created thus no constructor call."

So I used Query() instead. if ( qu.obj == BSONObj() ) works for testing if qu is empty.

My final function is:

void printQuery(DBClientConnection &c, const string &dc, Query &qu = Query(), const string &sortby = "" )

I couldn't make the DBClientConnection qualified as const. It raised the no legal conversion for 'this' pointer when using c.query and

C2662: 'mongo::DBClientWithCommands::count' : cannot convert 'this' pointer from 'const mongo::DBClientConnection' to 'mongo::DBClientWithCommands &' Conversion loses qualifiers

when using c.count. So I kept it unqualified.

Therefore
  • 367
  • 2
  • 13
0

You should be sorting the cursor and not on qu, which I presume to be your BSON query. eg.

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

Check out http://www.mongodb.org/pages/viewpage.action?pageId=133415#C%2B%2BTutorial-Sorting for more information.

slee
  • 524
  • 2
  • 6
  • It appears to sort on the query: `auto_ptr cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );` To sort on the query, it would read: `auto_ptr cursor = c.query("tutorial.persons", QUERY( "age" << age )).sort("name");` I changed my code to read `cursor = c.query( dc,qu).sort(sortby);` & it raised `error C2039: 'sort' : is not a member of 'std::auto_ptr<_Ty>'` Am I missing something fundamental? I thought sort was a helper function of the Query object. – Therefore Sep 13 '12 at 00:18
  • "To sort on the query, it would read:" should be "To sort on the cursor, it would read:" – Therefore Sep 13 '12 at 01:17