1

I have C# app and I added the nugget mongodb driver.

My mongodb database is called DevServers My collection is called server

So I'm trying to connect and then just insert a random document since I assume that is allowed with mongo , it doesn't match the other 3 in the collection/db

var connectionString = "mongodb://root:[email protected]:27017"; 
var client = new MongoClient(connectionString);

          var db = client.GetDatabase("DevServers");
            var collection = db.GetCollection<BsonDocument>("server");


            var document = new BsonDocument
            {
                {"Brand","Dell"},
                {"Price","400"},
                {"Ram","8GB"},
                {"HardDisk","1TB"},
                {"Screen","16inch"}
            };


            collection.InsertOneAsync(document);

I refresh the Studio 3T app and it does not show any inserts. No error in the try/catch, what am I doing wrong?

1 Answer 1

1

Looks like you're using the Async method without await. Your method has to be marked as async as well.

public async Task Save() {
   // rest of code here
   await collection.InsertOneAsync(document);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is the connection string info the same as what you're using in the Studio app? Is the mongod process still running?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.