【发布时间】:2019-01-03 04:52:27
【问题描述】:
我有以下文档,我正在尝试搜索嵌套文档“ids”中的元素,
如果找到元素返回值 bool 或 count。我尝试使用 Mongodb 查询,但不确定如何为数组元素编写查询。
错误:-Operator == cannot be applied to operands of type 'string[]' and 'string'
foreach (var req in _addUpdateRailsObjectInfoRequest.ids)
{
string value=req.id
var isIdExists = Builders<RailsData>.Filter.And(
Builders<RailsData>.Filter.ElemMatch(c => c.content.queryInclude, c => c.type == req.type),
Builders<RailsData>.Filter.ElemMatch(c => c.content.queryInclude, c => c.ids == value));
}
//c.id==value 附近的最后一行出错。运算符 == 不能应用于 'string[]' 和 'string' 类型的操作数
{
"_id" : ObjectId("5c2d3e700aff6771ebfc88ea"),
"name" : "Toyota",
"content" : {
"queryInclude" : [
{
"type" : "departments",
"ids" : [
"21",
"18",
"19",
"29",
"30"
]
}
]
}
}
//
public class RailsData
{
public string name { get; set; }
public content content { get; set; }
}
public class content
{
public List<queryInclude> queryInclude { get; set; }
}
public class queryInclude
{
public string type { get; set; }
public string[] ids { get; set; }
}
【问题讨论】:
-
这个怎么样:
Builders<RailsData>.Filter.ElemMatch(c => c.content.queryInclude, c => c.ids.Contains(value));?
标签: c# mongodb linq mongodb-query