2017-04-16 77 views
0

我有mongodb版本3.2.10與數據庫名稱「足球」和集合名稱「足球運動員」。無法查詢使用asp.net mongodb文檔

這裏是一個文件例如:

{ "_id" : ObjectId("58f2c4dfc9f8596b2e50c0c9"), "Name" : "Messi" } 

我試圖拉記錄並顯示在網頁上,但我不能得到這個代碼工作。 它返回錯誤

CS0411 The type arguments for method 'IMongoDatabase.GetCollection<TDocument>(string, MongoCollectionSettings)' 

不能從使用推斷。嘗試明確指定類型參數。

代碼:

using MongoDB.Bson; 
using MongoDB.Driver; 

protected void btnSave_Click(object sender, EventArgs e) 
     { 
      var connectionstring = "mongodb://localhost"; 
      var client = new MongoClient(connectionstring); 
      var DB = client.GetDatabase("football"); 

      var collection = DB.GetCollection("Footballers"); 
      Response.Write(DB.GetCollection("Footballers")); 


     } 
+0

是一個錯字'DB.GetCollection( 「足球」)'? 'GetCollection'後面的點? – Veeram

+0

@Veeram我在發佈我的代碼時發生了錯誤。我刪除了。並且代碼就是我正在嘗試運行的結果,這是我在帖子中更新的錯誤消息。 – ikask

回答

0

嘗試BsonDocument

var collection = DB.GetCollection<<BsonDocument>("Footballers"); 
var document = collection.Find(Builders<BsonDocument>.Filter.Eq("Name", "Messi")).First(); 

http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#get-a-collection

http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#get-a-single-document-with-a-filter

+0

我得到「var document = ...」的運行時錯誤System.InvalidOperationException發生 Message = Sequence不包含元素 Source = MongoDB.Driver.Core – ikask

+0

對不起,這是我的查詢錯字。現在從'name'改爲'Name'。現在試試。 – Veeram

+0

謝謝,這適用於單個文檔查詢,但是,在我的代碼中,我試圖返回Footballers集合中的所有文檔。 – ikask