英文:
MongoDB - Filter data matching one of the options
问题
你正在尝试使用一个BsonDocument
编写的查询来过滤MongoDB数据,其中一个参数 - 例如“name” - 等于可能值数组中的一个,但无法获得正确的语法:
例如,“name”等于“bill”或“name”等于“fred”
我尝试使用以下格式:
query = new BsonDocument
{
{"name", new BsonDocument {
{ "$in", new BsonArray { "bill", "fred" } }
}}
};
var entities = await collection.Find(query).ToListAsync();
但我收到错误信息:
System.InvalidOperationException: '重复的元素名称“$in”。'
我相信答案相当简单,但无法完全理解。
英文:
I'm trying to filter MongoDB data using a query written using a BsonDocument
where a parameter - eg "name" - equals one of an array of possible values, but can't get the correct syntax for this:
Eg "name" equals "bill" or "name" equals "fred"
I've tried using the format:
query = new BsonDocument
{
{"name" , new BsonDocument {
{ "$eq" , "bill"},
{ "$eq" , "fred"}
}}
};
var entities = await collection.Find(query).ToListAsync();
But I get the error:
> System.InvalidOperationException: 'Duplicate element name '$eq'.'
I'm sure the answer is pretty simple, but can't quite nail it.
答案1
得分: 1
根据您提供的内容,翻译如下:
对于与解析出的任何名称匹配的情况,您应该应用$in
运算符并提供一个列表/数组。
query = new BsonDocument
{
{ "name", new BsonDocument {
{ "$in", BsonArray.Create(new List<string> { "bill", "free" }) },
}}
};
英文:
For your scenario which matches any of the parsed names, you should apply the $in
operator and provide a list/array.
query = new BsonDocument
{
{ "name", new BsonDocument {
{ "$in", BsonArray.Create(new List<string> { "bill", "free" }) },
}}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论