12

I've picked up a piece of code that is using the MongoDB driver like this to get a single object from a collection...this can't be right, can it? Is there a better way of getting this?

IMongoCollection<ApplicationUser> userCollection;
....
userCollection.FindAsync(x => x.Id == inputId).Result.ToListAsync().Result.Single();
i3arnon
  • 113,022
  • 33
  • 324
  • 344
Paul
  • 9,409
  • 13
  • 64
  • 113

4 Answers4

31

Yes, there is.

First of all don't use FindAsync, use Find instead. On the IFindFluent result use the SingleAsync extension method and await the returned task inside an async method:

async Task MainAsync()
{
    IMongoCollection<ApplicationUser> userCollection = ...;

    var applicationUser = await userCollection.Find(_ => _.Id == inputId).SingleAsync();
}

The new driver uses async-await exclusively. Don't block on it by using Task.Result.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • Also, rather than having to catch the `InvalidOperationException` on a miss consider `FirstOrDefaultAsync()` instead of `SingleAsync()` – KCD May 24 '16 at 05:51
  • 4
    @KCD `Single` enforces there's only a single matching item and no more. Using `First` can hide data errors. If you want to support a case when there's not even a single matching item you can use `SingleOrDefaultAsync` – i3arnon May 24 '16 at 14:24
  • 4
    I'm using the latest driver. Should we do Find+SingleAsync, or FindAsync+SingleAsync, or is it the same thing in the end? – Simon Mourier Oct 10 '18 at 13:57
8

You should limit your query before executing, otherwise you will first find all results and then only read one of it.

You could either specify the limit using FindOptions in FindAsync, or use the fluent syntax to limit the query before executing it:

var results = await userCollection.Find(x => x.Id == inputId).Limit(1).ToListAsync();
ApplicationUser singleResult = results.FirstOrDefault();

The result from ToListAsync will be a list but since you limited the number of results to 1, that list will only have a single result which you can access using Linq.

poke
  • 369,085
  • 72
  • 557
  • 602
  • 4
    Why do you use limit in query with condition by primary key? Why do you use ToListAsync and then FirstOrDefault? I don't understand who could upvote this answer. – rnofenko Jul 01 '15 at 22:03
  • 1
    @rnofenko (1) `Id` is not guaranteed to be a primary key I think. (2) I was not aware of the existence of `IFindFluentExtensions`, as such I couldn’t know about `SingleAsync`. (3) The implementation behind `SingleAsync` does use a limit too. (4) `ToListAsync` is one way to execute an `IFindFluent` query and simpler to use than the cursor. (5) `ToListAsync` still returns a list, so if you want to get the actual item, you need to access that first, e.g. using `FirstOrDefault`. – poke Jul 01 '15 at 22:10
  • @rnofenko I know about `_id` but after reading through the documentation I found no proof yet that this is always mapped to the `Id` property in .NET. Instead, I have something that suggests that [this does not always need to be the case](http://api.mongodb.org/csharp/current/html/T_MongoDB_Bson_Serialization_Attributes_BsonIdAttribute.htm). – poke Jul 02 '15 at 06:29
  • Id Mapping documentation: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/#the-id-member – Craig Wilson Jul 03 '15 at 13:45
  • 1
    Note that `.Find(...).FirstOrDefault()` is already provided, and it does a `.Limit(1)` under the covers: https://github.com/mongodb/mongo-csharp-driver/blob/45c67f553f7e1bf820e5a50b9cf8e581e6bd9bdf/src/MongoDB.Driver/IFindFluentExtensions.cs#L213-L218 – Simon MᶜKenzie Sep 01 '22 at 02:01
2

In newer versions of MongoDB Find() is deprecated, so you can either use

collection.FindSync(o => o.Id == myId).Single()

or

collection.FindAsync(o => o.Id == myId).Result.Single()

You can also use SingleOrDefault(), which returns null if no match was found instead of throwing an exception.

Nick
  • 472
  • 3
  • 18
0

I could not get the method:

coll.Find(_ => _.Id == inputId).SingleAsync();

To work as I was getting the error

InvalidOperationException: Sequence contains more than one element c#

So I ended up using .FirstOrDefault()

Example:

public FooClass GetFirstFooDocument(string templatename)
        {
            var coll = db.GetCollection<FooClass>("foo");
            FooClass foo = coll.Find(_ => _.TemplateName == templatename).FirstOrDefault();
            return foo; 
        }
l33tHax0r
  • 1,384
  • 1
  • 15
  • 31