C# 选择具有嵌入文档筛选值的文档
你能告诉我怎么做C#等价物吗C# 选择具有嵌入文档筛选值的文档,c#,mongodb,aggregation,C#,Mongodb,Aggregation,你能告诉我怎么做C#等价物吗 db.UserProfile.aggregate([ {$match:{_id:"sen"}}, { $project: { DemRole: { $filter: { input: "$DemRole", as: "item", cond: { $eq:
db.UserProfile.aggregate([
{$match:{_id:"sen"}},
{
$project: {
DemRole: {
$filter: {
input: "$DemRole",
as: "item",
cond: { $eq: [ "$$item.Name", "CO" ] }
}
}
}
}
])
我正在尝试选择一个与_id匹配的文档,并在对嵌入文档应用过滤器后检索结果。它在Robo3T上的MongoDB中运行良好。但我无法用C#翻译相同的内容。这应该会让你开始:
var collection = new MongoClient().GetDatabase("test").GetCollection<User>("UserProfile");
var pipeline = collection.Aggregate()
.Match(up => up.Id == "sen")
.Project(up => new { DemRole = up.DemRole.Where(c => c.Name == "CO") });
var collection=new MongoClient().GetDatabase(“test”).GetCollection(“UserProfile”);
var pipeline=collection.Aggregate()
.Match(up=>up.Id==“sen”)
.Project(up=>new{devrole=up.devrole.Where(c=>c.Name==“CO”)});
谢谢你的回答。它现在在C#中工作。但下面是我如何获得信息的
var pipeline = collection.Aggregate()
.Match(up => up.UserID == userId)
.Project(up => new { DemRole = up.DemRole.Where(c => c.Status== true) }).ToList();
// .Project(dem => dem.DemRole.Where(c => c.Status == true));//.ToList();
foreach(var pipe in pipeline)
{
return pipe.DemRole.ToList();
}
我想知道下面的代码执行的操作
.Project(up => new { DemRole = up.DemRole.Where(c => c.Status== true) })
以及为什么下面的代码不起作用
.Project(dem => dem.DemRole.Where(c => c.Status == true));//.ToList();
此外,我还必须运行foreach来获取下面给出的信息
foreach(var pipe in pipeline)
{
return pipe.DemRole.ToList();
}
如果你能解释以上几行,或者给我指一些我可以阅读的文档,那就更好了