MongoDB多字段查找

时间:2018-10-04 13:32:42

标签: mongodb mongodb-query aggregation-framework

我正在尝试使用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'

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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"
         }
    }
])

答案 2 :(得分:0)

ActiveWorkbook.Queries.Add Name:="CONFIG", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Access.Database(File.Contents(""C:\Users\Astrashar\Desktop\Projet\Test.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & "    _CONFIG = Source{[Schema="""",Item=""CONFIG""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    _CONFIG"

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""CONFIG"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [CONFIG]")
        .ListObject.DisplayName = "CONFIG"
        .Refresh BackgroundQuery:=False
End With