Take the 2-minute tour ×

I would like to know how can I check the existence of an object with mongoDB and C#.

I've found a way to do it but I had to use Linq thanks to Any() method, but I'd like to know if it's possible to do it without Linq ?

database.GetCollection<ApplicationViewModel>("Applications").Find(Query.EQ("Name", applicationName)).Any()

Thanks guys!

share|improve this question
Why without LINQ? –  Mauro Jun 20 '11 at 12:42
Without Linq because I dont want to process my list of elements in memory. –  Gui Jun 20 '11 at 12:43
add comment

3 Answers

up vote 5 down vote accepted

Use $count operator to avoid memory issues, it not loading documents from database into memory:

int count = items.FindAs<LedgerDocument>(Query.EQ("name", appName)).Count();

if(count > 0)
   //then doc exists

Operator $exists in mongodb can be used to identfy that some field exists in a document, but you can't pass query to it:

                  .Find(Query.Exists("Name", true));
share|improve this answer
Hi Andrew, how do we filter with what you wrote ? I mean, How can I specify that I want to know if any LedgerDocument.Name == SomethingName exists ? –  Gui Jun 20 '11 at 14:01
@JohnSmith: Sorry it was my mistake, see i updated my answer with correct code. –  Andrew Orsich Jun 20 '11 at 14:13
thanks you so much for you answer, you made my day. Your code works absolutely fine. I give you 1 vote + Accepted answer. –  Gui Jun 20 '11 at 14:24
@JohnSmith: you are welcome. –  Andrew Orsich Jun 20 '11 at 15:58
add comment

MongoCollection.Exists checks whether the collection itself exists, not whether a particular document exists.

Query.Exists (the Query builder version of $exists) is used to query whether a document contains a particular field (by name).

There is no "official" way to query whether a document that matches a query exists or not, but the suggestion by Andrew Orsich to use count is probably the best way. They only comment I would add is that if you are going to process the matching document(s) anyway, then you might as well go ahead and query for them using some variation of Find.

share|improve this answer
add comment

I'll suggest the methods depicted in official tutorial


You can find and then count to get existence.

EDIT: For fixing memory issue, it seems it "exists" the Exists method in MongoCollection object ;)

share|improve this answer
If memory is the issue, I think this answer is not worth, right? –  Mauro Jun 20 '11 at 12:45
Edited for memory –  Mauro Jun 20 '11 at 12:48
the memory actually isn't really an issue in my case. I'm just curious to know if the C# driver from 10gen offer an "official" way to check for the existence of a document. Because I saw that with mongo syntax, there're the "exist" keyword as you can see here mongodb.org/display/DOCS/… but I don't know if there're something similar in C#... –  Gui Jun 20 '11 at 12:51
yeah I saw the Exist keyword from MongoCollection, but it doesn't take any parameter, so how can I filter my list of applications by a particular name ? –  Gui Jun 20 '11 at 12:53
add comment

Your Answer


