2015-09-19 232 views
0

我知道我可能會聽起來像你們一樣,但總的來說,我是。 我想在c#中使用mongodb驅動程序。嘗試做一些事情,比如添加一條記錄。MongoDB C#使用新的驅動程序

今天我學到了所有基本的MongoDB查詢,甚至用robomongo嘗試過。

但我不明白如何在c#中使用它? 如何從主函數調用它?

這是我寫的代碼(嘗試使用MongoDB的網站教程):

什麼是等待?什麼是任務?這是什麼意思,以及如何使其工作?

非常感謝您的幫助。

class Program 
{ 
    protected static IMongoClient _client; 
    protected static IMongoDatabase _database; 


    public static void Main() 
    { 
     _client = new MongoClient(); 
     _database = _client.GetDatabase("test"); 
     Task simpleTask = Tasky(); 
    } 
    public async Task Tasky() 
    { 
     var document = new BsonDocument 
      { 
       { "address" , new BsonDocument 
        { 
         { "street", "2 Avenue" }, 
         { "zipcode", "10075" }, 
         { "building", "1480" }, 
         { "coord", new BsonArray { 73.9557413, 40.7720266 } } 
        } 
       }, 
       { "borough", "Manhattan" }, 
       { "cuisine", "Italian" }, 
       { "grades", new BsonArray 
        { 
         new BsonDocument 
         { 
          { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) }, 
          { "grade", "A" }, 
          { "score", 11 } 
         }, 
         new BsonDocument 
         { 
          { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) }, 
          { "grade", "B" }, 
          { "score", 17 } 
         } 
        } 
       }, 
       { "name", "Vella" }, 
       { "restaurant_id", "41704620" } 
      }; 

         var collection = _database.GetCollection<BsonDocument>("restaurants"); 
         await collection.InsertOneAsync(document); 
        } 

} 
+0

???????????????????????????????????????? –

回答

0

嘗試瞭解更簡單的版本,然後閱讀有關async/await。一些文件添加到使用mongo外殼您的收藏和嘗試這個辦法:

var collection = new MongoClient("mongodb://localhost").GetServer() 
     .GetDatabase("your_db").GetCollection<BsonDocument>("your_collection"); 
var cursor = collection.FindAll(); 
foreach(var doc in cursor) { 
    Console.WriteLine(doc); 
} 

cursor用於獲取文件一個接一個。