Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:51:41+00:00 2026-06-10T22:51:41+00:00

Windows 7 64 SP1 — MongoDB 2.2.0-rc2 — Boost 1.42 — MS VS 2010

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:51:42+00:00Added an answer on June 10, 2026 at 10:51 pm
    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Windows 7 64 SP1 -- MongoDB 2.2.0-rc2 -- Boost 1.42 -- MS VS 2010
Environment: Windows 7 SP1 Visual Studio 2010 Ultimate SP1 .NET 4.0 I have a
In VB.net 3.5 SP1 I have a Windows Form with three DataGridView controls. One
I have a Windows 7 machine with Visual Studio 2005 SP1 installed. Using this,
I have a windows 2003 server (WEb edition) which has .net 3.5 sp1 installed
I'm using Windows Vista Ultimate and today I've installed the Visual Studio 2008 SP1,
I have a new WIndows Server 2008 R2 x64 DataCentre with Framework 3.5 SP1
I am using Visual Studio 2010 SP1 in Windows 7 64bit dev box. My
I have a problem with my newly installed VisualStudio 2008 SP1 on a Windows
I have done a WPF appliction using VS 2008 SP1 in Windows XP SP3.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.