C#中的MongoDB复合条件

时间:2011-04-01 16:54:32

标签: c# mongodb mongodb-.net-driver

如何在使用C#MongoDB驱动程序时创建复合条件?

这有效:

mongoCollection = mdb.GetCollection("person");
BsonElement be1=new BsonElement("surname","Jones");
qryPattern = new QueryDocument(new BsonElement[] {be1});
foreach (MongoDB.Bson.BsonDocument doc in mongoCollection.FindAs<MongoDB.Bson.BsonDocument>(qryPattern))
{
    rc.Append(doc.ToJson());
    rc.Append("<br />");
}

但是如何调整我的BsonElement以支持复合条件,例如

BsonElement be1=new BsonElement("surname","[Jones,Smith]");

甚至

BsonElement be1=new BsonElement("surname","Jones");
BsonElement be2=new BsonElement("surname","Smith");
qryPattern = new QueryDocument(new BsonElement[] {be1,be2});

非常感谢

1 个答案:

答案 0 :(得分:1)

这很简单,您应该使用Query.In

var names = new List<string>();
names.Add("Jones");
names.Add("Smith");
var query = Query.In("surname", BsonArray.Create(names));
collection.FindAs<BsonDocument>(query);