c# - C# 中的 MongoDB 复合条件

标签 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});

非常感谢

最佳答案

很简单,你应该使用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);

关于c# - C# 中的 MongoDB 复合条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5516435/

相关文章:

c# - 如果目录不存在,我如何使用 FileSystemWatcher 来监视目录?

c# - C# : what will happen in this edge case? 中的通用扩展方法

MongoDB C# 驱动程序 'Cursor not found'

MongoDB Mongoose 使用 Map 查找

c# - pbkdf2_sha256 C# 实现

c# - 检测意外的套接字断开连接

MongoDB 外壳 : How to convert array fields into string

c# - 预测 MongoDb 中字符串的索引键长度

C# MongoDB IAsyncCursor 详解

c# - MongoDB .NET 驱动程序 - StartsWith 和 Contains 包含松散类型数据