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

  • Home
  • SEARCH
  • 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 7507097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:25:04+00:00 2026-05-29T22:25:04+00:00

Using the c# driver for MongoDB I can easily construct a query against which

  • 0

Using the c# driver for MongoDB I can easily construct a query against which I can then add SetSkip() and SetLimit() parameters to constrict the result set to a certain size.

However I’d like to be able to know what item count of the query would be before applying Skip and Take without executing the query and loading the entire result set (which could be huge) into memory.

It looks like I can do this with MongoDB directly through the shell by using the count() command. e.g.:

 db.item.find( { "FieldToMatch" : "ValueToMatch" } ).count()

Which just returns an integer and that’s exactly what I want. But I can’t see a way in the documentation of doing this through the C# driver. Is it possible?

(It should be noted that we’re already using the query builder extensively, so ideally I’d much rather do this through the query builder than start issuing commands down to the shell through the driver, if that’s possible. But if that’s the only solution then an example would be helpful, thanks.)

Cheers,
Matt

  • 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-05-29T22:25:05+00:00Added an answer on May 29, 2026 at 10:25 pm

    You can do it like this:

    var server = MongoServer.Create("mongodb://localhost:27020");
    var database = server.GetDatabase("someDb");
    
    var collection = database.GetCollection<Type>("item");
    var cursor = collection.Find(Query.EQ("FieldToMatch" : "ValueToMatch"));
    
    var count = cursor.Count(); 
    

    Some notes:

    1. You should have only one instance of server (singleton)
    2. latest driver version actually returns long count instead of int
    3. Cursor only fetches data once you iterate
    4. You can configure a lot of things like skip, take, specify fields to return in cursor before actually load data (start iteration)
    5. Count() method of cursor loads only document count
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the MongoDB Java driver, and can't seem to get a query to
How do I query MongoDB for nearby geographic points using the C# driver and
Using the ruby mongodb driver, is there a way I can create a connection
I am using MongoDB with C# driver. I managed to add/delete/update data from collections,
I've a C++ function which saves a document to MongoDB using C++ driver. It
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
I am using MongoDB v1.4 and the mongodb-csharp driver and I try to group
On linux, it's possible to create a tun interface using a tun driver which
I am using the SQL Server PHP Driver, I think this question can be
I am using 10Gen .net driver for MongoDB and querying objects that has empty

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.