我可以在MongoDB的$ graphlookup操作中使用“_id”字段吗?

时间:2017-09-22 21:02:59

标签: mongodb spring-data-mongodb

是否可以使用“id”(字符串)属性在graphlookup中匹配?使用任何其他字符串属性它似乎工作。

e.g。下面的查询返回空“groups”(其中members.userId == id)

Collection1:
{ 
    "_id" : "U2", 
    "displayName" : "testName"
}

Collection2:
{ 
    "_id" : "UG1", 
    "members" : [
        {
            "userId" : "U2", 
             "admin" : false
        }
    ], 
    "name" : "testGroup"
}

db.collection1.aggregate( [
    {
      "$match": {
        "_id": "U2"
      }
    },
    {
      "$graphLookup": {
        "from": "collection2",
        "startWith": "$id",
        "connectFromField": "_id",
        "connectToField": "members.userId",
        "as": "groups"
      }
    }
  ]
);

但是下面的查询将返回匹配的“groups”(其中members.userId == displayName)

db.collection1.aggregate( [
    {
      "$match": {
        "_id": "U2"
      }
    },
    {
      "$graphLookup": {
        "from": "collection2",
        "startWith": "$displayName",
        "connectFromField": "displayName",
        "connectToField": "members.userId",
        "as": "groups"
      }
    }
  ]
);

更新

下面的查询工作正常(必须在startwith子句中添加 $ ):

db.collection1.aggregate( [
    {
      "$match": {
        "_id": "U2"
      }
    },
    {
        $graphLookup: {
            from: "collection2",
            startWith: "$_id", 
            connectFromField: "_id",
            connectToField: "members.userId",
            as: "groups"
         }
    }
  ]
);

0 个答案:

没有答案