此问题涉及How to project specific fields in array on filtered lookup中的示例 过滤器应用于'加入'表。 现在我想将其转换为C#中的查询,但我没有设法将过滤器添加到投影中。这是我得到的:
db.GetCollection("meta")
.Aggregate()
.Match(new BsonDocument { { "test", "OK" }})
.Lookup("merge","Exp","Exp","kin")
.Project(Builders<BsonDocument>.Projection
.Include("Exp")
.Include("test")
.Include("kin")
)
有什么想法吗?
答案 0 :(得分:1)
似乎工作艰难:
db.GetCollection("meta")
.Aggregate()
.Match(new BsonDocument { { "test", "OK" }})
.Lookup("merge","Exp","Exp","kin")
.Project(new BsonDocument {
{ "Exp" , 1},
{ "test" , 1},
{ "kin", new BsonDocument {
{ "$filter", new BsonDocument {
{ "input", "$kin"},
{ "as", , "kin"},
{ "cond", new BsonDocument {
{ "$eq", new BsonArray { "$$kin.M2", "val"}}}
}}
}}
}})
.Project(new BsonDocument {
{ "Exp", 1 },
{"test", 1},
{"date", 1},
{"kin.M1",1},
{"kin.M2",1},
{"kin.T",1 }})
但当然使用Mongo Driver的API会很好。