2

使用 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});

非常感谢

4

1 回答 1

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);
于 2011-04-01T17:15:29.497 回答