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

bsxbgnwa  于 2023-01-12  发布在  Go
关注(0)|答案(1)|浏览(114)

这是我的代码:

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集合,MongoDB. Driver. FilterDefinition,MongoDB. Driver. FindOptions)"具有一些无效参数

pjngdqdw

pjngdqdw1#

您的QueryDocument不包含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();

相关问题