Insert new document using InsertOneAsync (.NET Driver 2.0)

308 views
Skip to first unread message

Hana

unread,
Apr 18, 2015, 8:13:14 AM4/18/15
to mongo...@googlegroups.com

Refs :

Introducing the 2.0 .NET Driver

Reading and Writing

 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();
       Console.WriteLine("State: " + tsk.Status);            
}

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

Craig Wilson

unread,
Apr 18, 2015, 8:42:06 AM4/18/15
to mongo...@googlegroups.com
Hi Hana,

This group is used to talk about development of MongoDB itself. Your question is better suited for the mongodb-user or mongodb-csharp groups.

To answer your question, you must wait on the Task returned from insert to finish. Repository.Insert().Wait();  I'd suggest you have a read up on async programming in .NET here: https://msdn.microsoft.com/en-us/library/hh191443.aspx.

Craig
Reply all
Reply to author
Forward
0 new messages