同一集合中的$ lookup是嵌套文档而不是返回所有文档

时间:2017-11-26 16:32:24

标签: mongodb

处理$lookup很有趣,直到我想到使用相同的集合进行连接。

说我有下一个系列:

{'_id': ObjectId('5a1a62026462db0032897179'),
'department': ObjectId('5a1982646462db032d58c3f9'),
'name': 'Standards and Quality Department',
'type': 'sub'}, {
'_id': ObjectId('5a1982646462db032d58c3f9'),
'department': false,
'desc': 'Operations Department',
'type': 'main'}

正如它所说的那样,使用department密钥在同一个集合中进行反向链接,可以false表示最高级别的部门。

我正在使用下一个查询(Python)来填充结果:

query = [{'$lookup': {'as': '__department',
              'foreignField': '_id',
              'from': 'departments',
              'localField': 'department'}},
 {'$unwind': '$__department'},
 {'$group': {'__department': {'$first': '$__department'},
             '_id': '$_id',
             'department': {'$first': '$department'},
             'name': {'$first': '$name'},
             'type': {'$first': '$type'}}}]
for doc in conn.db.departments.aggregate(query): pprint(doc)

我期待得到的东西:

{'__department': None,
 '_id': ObjectId('5a1982646462db032d58c3f9'),
 'department': false,
 'name': 'Operations Department',
 'type': 'main'},
{'__department': {'_id': ObjectId('5a1982646462db032d58c3f9'),
                  'department': 'false',
                  'name': 'Operations Department',
                  'type': 'main'},
 '_id': ObjectId('5a1a62026462db0032897179'),
 'department': ObjectId('5a1982646462db032d58c3f9'),
 'name': 'Standards and Quality Department',
 'type': 'sub'}

我实际得到的是:

{'__department': {'_id': ObjectId('5a1982646462db032d58c3f9'),
                  'department': 'false',
                  'name': 'Operations Department',
                  'type': 'main'},
 '_id': ObjectId('5a1a62026462db0032897179'),
 'department': ObjectId('5a1982646462db032d58c3f9'),
 'name': 'Standards and Quality Department',
 'type': 'sub'}

我不确定为什么$unwind将两个文档分组在一起虽然在应用$unwind之前我确实将它们分开了。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

这是因为您在文档中创建了一个空数组$lookup,但未在{ "_id" : ObjectId("5a1982646462db032d58c3f9"), "department" : false, "desc" : "Operations Department", "type" : "main", "__department" : [] } 中找到匹配项。这就是您的孤儿文档的样子:

$unwind

当您展开时,此文档中没有$lookup,因此在此过程中会丢失。如果你想保留它,你必须"标准化"你的阵列。因此,您必须在$unwind之后和{ $project: { _id: 1, department: 1, name: 1, type: 1, __department: { $cond: [{ $eq: ["$__department", []] }, [{ _id: 0, department: "None", desc: "None", type: "None" }], '$__department' ] } } } 之前添加此内容:

[{
        '$lookup': {
            'as': '__department',
            'foreignField': '_id',
            'from': 'depart',
            'localField': 'department'
        }
    },
    {
        '$project': {
            _id: 1,
            department: 1,
            name: 1,
            type: 1,
            __department: {
                $cond: [{
                        $eq: ["$__department", []]
                    },
                    [{
                        _id: 0,
                        department: "None", 
                        desc: "None",
                        type: "None"
                    }], '$__department'
                ]
            }
        }
    },
    {'$unwind': "$__department"},
    {'$group': {'__department': {'$first': '$__department'},
             '_id': '$_id',
             'department': {'$first': '$department'},
             'name': {'$first': '$name'},
             'type': {'$first': '$type'}}}]

所以它们应该是这样的:

read_template_file'", "/usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-output-elasticsearch-7.4.2-java/lib/logstash/outputs/elasticsearch/template_manager.rb:23:in