【问题标题】:MongoDB multiple fields lookupMongoDB多字段查找
【发布时间】:2018-10-05 15:35:18
【问题描述】:

我正在尝试使用 MongoDB 3.6 上的两个字段执行 $lookup。我已经阅读了文档和 similar questions here,但我无法找到问题所在。

收藏acls:

[ { _id: 1, FolderId: 4, Sid: 'S-123-456' }
  { _id: 2, FolderId: 5, Sid: 'S-234-567' }
  { _id: 3, FolderId: 6, Sid: 'S-345-678' } ]

收藏groups:

[ { _id: 1, ProcessId: 10, Sid: 'S-123-456', Users: [ 'user1', 'user2'] }
  { _id: 2, ProcessId: 10, Sid: 'S-234-567', Users: [ 'user1'] }
  { _id: 3, ProcessId: 20, Sid: 'S-123-456', Users: [ 'user2'] } ]

查询:

db.acls.aggregate({
    $lookup: 
    { 
        from: 'groups',
        let: { 'ProcessId': 10, 'GroupSid': '$Sid' },
        pipeline: [{
                   $match: {
                      $expr: {
                         $and: [
                            {
                               $eq: [ '$ProcessId', '$$ProcessId' ]
                            },
                            {
                               $eq: [ '$Sid', '$$GroupSid' ]
                            }
                         ]
                      }
                   }
                }],
        as: 'grouplist'
    }
})

我期待返回类似:

{ _id: 1, FolderId: 4, Sid: 'S-123-456', 
  grouplist: [ { _id: 1, ProcessId: 10, Sid: 'S-123-456', Users: [ 'user1', 'user2'] }] }

但我在 Robo 3T 上收到了 'Script executed successfully, but there are no results to show'

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    试试这个,它工作正常。 您的 let 关键字必须以小写开头

        db.acls.aggregate([
       {
          $lookup:
             {
               from: "groups",
               let: { processid: 10, sid: "$Sid" },
               pipeline: [
                  { $match:
                     { $expr:
                        { $and:
                           [
                             { $eq: [ "$ProcessId",  "$$processid" ] },
                             { $gte: [ "$Sid", "$$sid" ] }
                           ]
                        }
                     }
                  }
               ],
               as: "grouplist"
             }
        }
    ])
    

    【讨论】:

    • 成功了!我在文档中找不到let 变量应该以小写字符开头的任何地方,但这是主要问题。非常感谢!
    【解决方案2】:

    $let variable 运算符必须以小写字母开头。

    db.acls.aggregate([
      { "$lookup": { 
        "from": 'groups',
        "let": { "groupSid": "$Sid" },
        "pipeline": [
          { "$match": {
            "$expr": { "$eq": [ "$Sid", "$$groupSid" ] },
            "ProcessId": 10
          }}
        ],
        "as": "grouplist"
      }}
    ])
    

    【讨论】:

    • 同样的消息:'Script executed successfully, but there are no results to show'
    • 更新答案请看
    • 工作正常,但我接受另一个答案,因为他指出了罪魁祸首:let 变量应该以小写字符开头。非常感谢您的宝贵时间。
    • 好的,没问题。您可以通过链接查看更新的答案
    【解决方案3】:
    db.getCollection("acls").aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $lookup: // Equality Match
                {
                    from: "groups",
                    localField: "Sid",
                    foreignField: "Sid",
                    as: "grouplist"
                }
    
    
            },
    
            // Stage 2
            {
                $project: {
                    grouplist: {
                        $filter: {
                            input: "$grouplist",
                            as: "group",
                            cond: {
                                $eq: ["$$group.ProcessId", 10]
                            }
                        }
                    },
    
                    FolderId: 1,
                    Sid: 1
                }
            },
    
        ]
    
    
    
    );
    

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2021-09-20
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多