-2

I would like to asynchronously check for existence of a document in MongoDB using the MongoDB C#/.NET 2.0 Driver. I would like to query in such a way that performance maximization is considered.

For example, we started with

db.Collection.CountAsync(filter).Result;

which would not be good practice for performance, particularly if the filter can return numerous documents.

Something like

db.Collection.FindAsync(filter).Limit(1).Size();

would be better, but FindAsync does not exist.

Additionally, I've seen reference to "field selection" that seems would be good to apply as well to minimize the amount data in motion, but MongoDB is new to me so I don't yet know whether I would unknowingly be causing degraded performance.

GaTechThomas
  • 5,421
  • 5
  • 43
  • 69
  • It would be very helpful if you included what you've tried and asked if it was the best way, rather than just asking for the code. In this case it's okay, but this is pretty close to being more appropriate for se.CodeReview – Codeman Apr 22 '15 at 21:50
  • *I would like to query in such a way that performance is maximized.* What does that mean to you? What would you call "maximized", performance wise? Note that async queries wont execute "faster" then synchronous ones. – Yuval Itzchakov Apr 22 '15 at 21:53
  • 2
    @Pheonixblade9 you mean *with the working code in the post*, right? Note that [CR's Help Center](http://www.codereview.stackexchange.com/help/on-topic) **explicitly** states that *asking "what is the best practice regarding X?"* is off-topic, so as currently stated, this question would get flamed on CR. What *is* appropriate is: posting your own working code, and asking *does this code adhere to best practice regarding X?* - there's quite a difference ;) – Mathieu Guindon Apr 22 '15 at 21:54
  • @Mat'sMug I meant that he should include working code - but I did not know that "best practice" questions were off topic for CR. TIL – Codeman Apr 22 '15 at 22:03
  • I've updated the question per comments above but then got no additional comments and subsequently the question was put on hold. Can I do something additional to make it acceptable? How is it that the following question seems to be fine and mine evokes the need to kill it?... http://stackoverflow.com/questions/9009987/improve-querying-fields-exist-in-mongodb – GaTechThomas Apr 24 '15 at 20:29
  • It would certainly be nice if a moderator would chime in here (see above comment). Yet another question asking about the most efficient way to do something... http://stackoverflow.com/questions/11355792/with-mongodb-and-guids-for-the-id-of-documents-what-is-efficient-way-to-store-th – GaTechThomas May 06 '15 at 17:45

1 Answers1

2

After doing some more digging around, I discovered the CountOptions parameter on the CountAsync method, which has the ability to limit the number of documents that are counted.

Limiting counting to a single document would do the trick for an equivalent to TSQL's "exists":

long count = db.Collection.CountAsync(filter, new CountOptions() { Limit = 1 }).Result;

bool exists = count > 0;

Edit:

Hopefully this C# extension method is helpful to someone:

public static bool Any<T>(this IMongoCollection<T> collection, FilterDefinition<T> filter)
{
  if (collection == null)
  {
    throw new ArgumentNullException("collection");
  }

  long count = collection.CountAsync(filter, new CountOptions() { Limit = 1 }).Result;

  return count > 0;
}

Usage...

  FilterDefinition<SomeDocClass> filter = Builders<SomeDocClass>.Filter.Eq(e => e.FieldName1, someValue);

  bool exists = _collection.Any(filter);
GaTechThomas
  • 5,421
  • 5
  • 43
  • 69
  • This blog post has performance considerations: https://blog.serverdensity.com/checking-if-a-document-exists-mongodb-slow-findone-vs-find/ – GaTechThomas Apr 28 '15 at 19:58