【发布时间】:2017-09-13 09:29:56
【问题描述】:
在我的 C# 控制台应用程序项目中,我使用 MongoDB.Driver.2.4.3 并连接到 MongoDB 3.2.10。
我收集了一些文档,我使用以下代码将其输出到控制台:
{ "_id" : ObjectId("58f034bf5c57ef10bc4f8d3d"), "firstname" : "Christano", "birthdate" : ISODate("1995-04-14T02:32:31Z"), "country" : "Brazil" } {“_id”:ObjectId(“58f0c7bb5c57ef10bc506216”),“名字”:“贝利”,“生日”:ISODate(“2007-05-14T02:32:31Z”),“国家”:“巴西”} {“_id”:ObjectId(“58f0ca995c57ef10bc506763”),“名字”:“安东尼”,“生日”:ISODate(“2010-04-14T02:32:31Z”),“国家”:“意大利”}
如何将各个字段值写入控制台,以便单独存储和操作这些值?
using MongoDB.Bson;
using MongoDB.Driver;
var playerfirstname;
var playerbirthdate;
var playercountry;
var client = new MongoClient("mongodb://localhost:27017");
var DB = client.GetDatabase("football");
var collection = DB.GetCollection<BsonDocument>("players");
var filter = Builders<BsonDocument>.Filter.Eq("country", "Brazil");
var cursor = collection.Find(filter).ToCursor();
foreach (var document in cursor.ToEnumerable())
{
Console.WriteLine(document);
}
【问题讨论】: