Reputation: 8499
I am using ASP.net Web API and MongoDB to create a simple service.
I am using the official Mongodb C# library.
How can I make it Async? I think the official Mongodb C# library does not support Async.
Can I just make the controller Async but not the select statement?
Controller:
public IQueryable<Test> GetAllPlaces()
{
return _test.GetAllPlaces().AsQueryable();
}
Select from mongodb database:
public IEnumerable<Test> GetAllPlaces()
{
return _test.FindAll();
}
Thank you.
Upvotes: 4
Views: 4823
Reputation: 5773
Bit old question, but a full async MongoDB driver for C# is coming up in Nov 2013:
https://jira.mongodb.org/browse/CSHARP-138
edit- As Eugene said, the driver is still under development. There are some experimental projects on Github, while we wait for the official one
https://github.com/rstam/mongo-async-csharp-driver https://github.com/andrebires/mongo-csharp-driver
02-Apr-2015 Update:
2.0 is out!: Nuget Link But be aware that async GridFS is not supported yet, you'll need to keep using the legacy package to work with it, until they release it, probably in version 2.1
(thanks paqogomez for the heads up)
Upvotes: 4
Reputation: 4602
Hopping on an old thread here, but FYI in C# you can let your queries run with SafeMode.False
as a parameter, (i beleive it's actually off by default) which will execute them in fire and forget manner.
My code for most of my stuff looks like this:
IMongoQuery query = Query.EQ("_id", Path);
IMongoUpdate update = Update.Set("Key", "value");
SafeModeResult oCmd = mCollection.Update(query, update, SafeMode.True);
Because i need safe mode. But if you set it to false or leave that paramter out, you'll get the fire and forget functionality.
Upvotes: 1
Reputation: 59763
While you could make it async, doing so won't provide you any real performance gains as the underlying library isn't Async. There's a lot more to it and is described well here. The general answer is "no."
Upvotes: 3