如何在C#和MongoDB version =“2.2.3”中动态添加过滤器到IMongoCollection?

时间:2016-05-21 04:37:40

标签: c# mongodb

这是我的代码:

var database = new MongoClient("mongodb://192.168.3.122").GetDatabase("xxxx");   
var collection = database.GetCollection<School>("School");

//var it = new List<IMongoQuery>();
//it.Add(Query.EQ("Phone", "77915656"));
//it.Add(Query.EQ("EstablishedYear", 1349));

//var query = new QueryBuilder<School>();
//query.And(it);

IMongoQuery query = new QueryDocument();

Query.And(query, Query.EQ("Phone", "77915656"));
Query.And(query, Query.EQ("EstablishedYear", 1349));
collection.Find(query).ToList(); // eror here!!!

我不知道如何从查询中获得结果?!

错误是:

  

'MongoDB.Driver.IMongoCollection'不包含'Find'的定义和最佳扩展方法重载'MongoDB.Driver.IMongoCollectionExtensions.Find(MongoDB.Driver.IMongo Collection,MongoDB.Driver.FilterDefinition,MongoDB。 Driver.FindOptions)'有一些无效的参数

1 个答案:

答案 0 :(得分:6)

您的Find不包含var database = new MongoClient("mongodb://192.168.3.122").GetDatabase("xxxx"); var collection = database.GetCollection<School>("School"); var builder = Builders<School>.Filter; var filter = builder.Eq("Phone", "77915656"); filter = filter & builder.Eq("EstablishedYear", 1349); var myFilteredList = collection.Find(filter).ToList(); 要求的类型...如果您想要创建过滤器,请使用过滤器构建器......我不在乎:

$(document).ready(function() {
      $('.project-name-block:eq(0)').slideDown('slow');
})