Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Mongo 3.6-解析嵌套对象列表中的引用对象_Mongodb_Aggregation Framework_Pymongo_Lookup - Fatal编程技术网

Mongodb Mongo 3.6-解析嵌套对象列表中的引用对象

Mongodb Mongo 3.6-解析嵌套对象列表中的引用对象,mongodb,aggregation-framework,pymongo,lookup,Mongodb,Aggregation Framework,Pymongo,Lookup,我有一个包含以下格式文件的集合: db.companys.find({}) 我还有一个用户集合,如下所示: db.users.find({}) 我尝试使用$lookup,以便最终查询的结果如下所示: { 'name': 'ABC Trading Ltd', 'telephone': '01292 480 485', 'comments': [{ 'user': { 'email': 'joe.bloggs@example.com

我有一个包含以下格式文件的集合:

db.companys.find({})

我还有一个用户集合,如下所示:

db.users.find({})

我尝试使用$lookup,以便最终查询的结果如下所示:

{
    'name': 'ABC Trading Ltd',
    'telephone': '01292 480 485',
    'comments': [{
        'user': {
              'email': 'joe.bloggs@example.com',
              'firstname': 'Joe',
              'lastname': 'Bloggs'
        },
        'date': 'example',
        'text': 'This is an example comment.'
     },
     {
        'user': {
              'email': 'jane.bloggs@example.com',
              'firstname': 'Jane',
              'lastname': 'Bloggs'
        },
        'date': 'example',
        'text': 'This is an example comment.'
     }] 
}
现在我知道,我可以使用以下方法查找值:

db.companies.aggregate[{"$lookup": {
           "from": "users",
           "localField": "user",
           "foreignField": "email",
           "as": "user"
        }}, {"$addFields": {
            "user": {"$arrayElemAt": ["$user", 0]}
        }}]
但是,只有当用户是文档中的顶级用户,并且只有一个字段需要替换时,这才有效。

请尝试以下操作:

db.companies.aggregate([{$unwind : '$comments'},{"$lookup": {
           "from": "users",
           "localField": "comments.user",
           "foreignField": "email",
           "as": "user"
        }}, {$addFields : {'comments.user': {"$arrayElemAt": ["$user", 0]}}},{$project : {user :0}}, 
        {$group:{_id:'$_id', comments:{$push : '$comments'}, data: {$first :'$$ROOT'}}}, {$addFields :{'data.comments':'$comments'}}, {$replaceRoot:{'newRoot': '$data'}}])

注意:通常我们不必对
$lookup
进行
$unwind
注释,除非需要,因为不同的值对象需要映射,另外,如果可能的话,尝试使用
$match
作为第一阶段和适当的索引,以便在
$lookup
上获得更好的性能。

那么,如果不使用$unwind,我将如何进行此操作?Match已经被使用了,我只是删除了它以简化我的问题。@Slepton:好吧,使用$Match作为第一阶段&如果你的数据集不太大(数百万个文档),那么你就可以开始了,我们不应该使用$unwind,我们必须在某些情况下使用它,这很正常,但我的建议是正确使用它,就像不在整个文档上一样,在过滤器($match)之后使用它应该足够好,只要您的查询性能良好,那么一切都正常:-)!!完美的是的,我实际上只检索单个文档,但需要使用聚合框架进行查找。
db.companies.aggregate[{"$lookup": {
           "from": "users",
           "localField": "user",
           "foreignField": "email",
           "as": "user"
        }}, {"$addFields": {
            "user": {"$arrayElemAt": ["$user", 0]}
        }}]
db.companies.aggregate([{$unwind : '$comments'},{"$lookup": {
           "from": "users",
           "localField": "comments.user",
           "foreignField": "email",
           "as": "user"
        }}, {$addFields : {'comments.user': {"$arrayElemAt": ["$user", 0]}}},{$project : {user :0}}, 
        {$group:{_id:'$_id', comments:{$push : '$comments'}, data: {$first :'$$ROOT'}}}, {$addFields :{'data.comments':'$comments'}}, {$replaceRoot:{'newRoot': '$data'}}])