2017-08-01 51 views
-2

這個問題涉及How to project specific fields in array on filtered lookup 中的示例,其中在「合併」表上應用了過濾器。 現在我想將它轉換爲C#中的查詢,但我無法將過濾器添加到投影中。這是我得到的:MongoDB在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") 
) 

任何想法?

回答

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 }}) 

但當然,這將是很好使用蒙戈驅動程序的API。