5

In the older .Net API version :

MongoClient client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("foo");
var collection = db.GetCollection<BsonDocument>("bar");
var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
collection.Save(document);

It worked.

When i use new .Net Driver 2.0 :

var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<BsonDocument>("bar");

var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
await collection.InsertOneAsync(document);

Error : The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Refs :

Introducing the 2.0 .NET Driver

Reading and Writing

I want to ask how to insert a new document using .Net Driver 2.0. Thanks.

[Update 1] I tried to implement :

public class Repository
{
    public static async Task Insert()
    {
        var client = new MongoClient("mongodb://localhost:27017");
        var database = client.GetDatabase("foo");
        var collection = database.GetCollection<BsonDocument>("bar");

        var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
        await collection.InsertOneAsync(document);
    }
}

static void Main(string[] args)
{            
       Task tsk = Repository.Insert();
       tsk.Wait();
       Console.WriteLine("State: " + tsk.Status);            
}

Result : WaitingForActivation. Nothing changed in database. Please help me!

[Update 2 (Solved)] : add tsk.Wait(); It worked ! Thanks this post : How would I run an async Task method synchronously?

Majix
  • 570
  • 7
  • 14
Hana
  • 824
  • 4
  • 14
  • 30

4 Answers4

3

Your method should be like

 public async void Insert()
    {
         var client = new MongoClient("mongodb://localhost:27017");
        var database = client.GetDatabase("foo");
        var collection = database.GetCollection<BsonDocument>("bar");

        var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
        await collection.InsertOneAsync(document);

    }
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • i tried as u said. Repository respository = new Repository(); Task tsk = respository.Insert(); Console.WriteLine("State: " + tsk.Status); Result : WaitingForActivation. – Hana Apr 13 '15 at 07:25
  • Nothing changed in database ! I dont know why. I never used async or task before so i dont know why. Can u help me ? – Hana Apr 13 '15 at 07:32
  • ya sure but do one thing please make sure your connection is working fine I mean you can fetch the data from mongo ? because async and await is nothing but it's just free your main thread – Dhaval Patel Apr 13 '15 at 07:35
  • Tks for your help! i add : tsk.Wait();. It worked. Hmm! Im really bad at synchronise. – Hana Apr 13 '15 at 15:59
3
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<BsonDocument>("bar");

var document = new BsonDocument { { "_id", 1 }, { "x", 2 } };
Task task = collection.InsertOneAsync(document);
task.Wait();

// From here on, your record/document should be in the MongoDB.

0

You can find in MongoDB C# driver meta file that all function declared without async which is required by await keyword and causes:

Error : The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

You can just delete the await key word. It works for me

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Alex
  • 1
0

The reason you saw nothing on the database at first was because you didn't wait (await) for Insert method to finish, which you later did by calling task.Wait(). As mentioned in comment in the link to the answer you provided, calling .Wait() like that can cause deadlock. Instead, you should call await Repository.Insert().

Check out this post about await-async http://blog.stephencleary.com/2012/02/async-and-await.html

msrc
  • 663
  • 1
  • 9
  • 19