2016-08-20 68 views
1

我有以下查詢我所測試和工程,但mgo我怎樣寫以下蒙戈聚集查詢使用的MgO Golang

var userId = "57a944390b1acf0d069388c1"; 
db.users.aggregate([ 
     { "$match": { "_id": userID } }, 
     { "$unwind": "$groups" }, 
     { 
      "$lookup": { 
       "from": "groups", 
       "localField": "groups.id", 
       "foreignField": "_id", 
       "as": "group" 
      } 
     }, 
     { "$unwind": "$group" }, 
     { 
      "$project": { 
       "group.requests": { 
        "$filter": { 
         "input" : "$group.requests", 
         "as" : "item", 
         "cond": { "$and" : [ 
           { "$ne" : ["$$item.user_id", userID] }, 
           { "$not" : { 
             "$setIsSubset" : [ 
              [userID], "$$item.denied_users" 
             ] 
            } 
           } 
          ]    
         } 
        } 
       } 
      } 
     }, 
     { "$unwind" : "$group.requests" } 
     ]) 

mgo查詢如下所示:

c := session.DB(info.Db()).C("users") 

    o1 := bson.M{"$match": bson.M{"_id": userID}} 
    o2 := bson.M{"$unwind": "$groups"} 
    o3 := bson.M{"$lookup": bson.M{ 
     "from":   "groups", 
     "localField": "groups.id", 
     "foreignField": "_id", 
     "as":   "group", 
    }} 
    o4 := bson.M{"$unwind": "$group"} 
    o5 := bson.M{ 
     "$project": bson.M{ 
      "group.requests": bson.M{ 
       "$filter": bson.M{ 
        "input": "$group.requests", 
        "as": "item", 
        "cond": bson.M{ 
         "$and": []bson.M{ 
          bson.M{"$ne": []string{"$$item.user_id", userID}}, 
          bson.M{"$not": bson.M{ 
           "$setIsSubset": []interface{}{ 
            []string{userID}, 
            []string{"$$item.denied_users"}, 
           }, 
          }}, 
         }, 
        }, 
       }, 
      }, 
     }, 
    } 
    o6 := bson.M{"$unwind": "$group.requests"} 
    pipeline := []bson.M{o1, o2, o3, o4, o5, o6} 

我的猜測是$filter不起作用。具體來說,我指定$setIsSubset和參數的方式與上面的實際mongo查詢不匹配。如果我刪除該部分(一切$not內,然後它的作品(除了不被正確的過濾器)。

基本上,我需要翻譯成MgO上查詢。

回答

1
"$setIsSubset": []interface{}{ 
    []string{userID}, 
    []string{"$$item.denied_users"}, 
}, 

等於

"$setIsSubset" : [ 
    [userID], ["$$item.denied_users"] 
] 

所以我覺得你需要定義它像

"$setIsSubset": []interface{}{ 
    []string{userID}, 
    "$$item.denied_users", 
},