string connectionString = "mongodb://10.10.32.125:27017";
MongoClientSettings settings = MongoClientSettings.FromUrl(new
MongoUrl(connectionString));
MongoClient mongoClient = new MongoClient(settings);
var Server = mongoClient.GetDatabase("mongovaibhav");
var collection = Server.GetCollection<employee>("mongov");
employee cd = new employee();
foreach (employee employess in collection.Find({ fname: "vaibhav" }) )
{
}
我在foreach
循环和MongoCollection
得到错误。我怎样才能获得数据列表? This error is showing
你创建了一个filter
var filter = Builders<employe>.Filter.Eq("fname", "vaibhav")
然后,您可以使用filter
访问您的数据
第一种选择
foreach (employee employess in
collection.Find(filter).ToListAsync().Result )
{
//do your computation
}
第二种选择
var employes = await collection.Find(filter).ToListAsync();
foreach(employe in employes)
{
//do your computation
}
string connectionString = "mongodb://10.10.32.125:27017";
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
MongoClient mongoClient = new MongoClient(settings);
var Server = mongoClient.GetDatabase("mongovaibhav");
var collection = Server.GetCollection<employee>("mongov");
var filter = Builders<employee>.Filter.Eq("fname", "vaibhav");
List<employee> emp = new List<employee>();
foreach (var emp1 in collection.Find(filter).ToListAsync().Result )
{
emp.Add(emp1);
}
GridView1.DataSource = emp;
GridView1.DataBind();
我得到解决方案谢谢大家和特别的@edkeveked