Mongodb $lookup 将集合合并成一个新集合

标签 mongodb mongodb-query aggregation-framework

首先,我是 mongodb 的新手,所以我的问题可能很愚蠢。

我有两个集合,我使用 $lookup

合并了它们
db.information.aggregate([     
    {       
        $lookup: {           
            from: "extra_info",
            localField: "identification", 
            foreignField: "identification",  
            as: "extra_info"         
        }    
    } 
])

作为输出,我得到了我想要的“集合”,但它没有存储在 db.information 集合。

有什么提示可以实现吗?

最佳答案

使用$out在聚合管道中将聚合数据保存到新集合中:

db.information.aggregate([     
{       
    $lookup: {           
        from: "extra_info",
        localField: "identification", 
        foreignField: "identification",  
        as: "extra_info"         
    }    
},
{ 
    $out : "newcollection"   // name of the collection to save the lookup in 
}
])

关于Mongodb $lookup 将集合合并成一个新集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39328811/

相关文章:

node.js - MongoDB 中的日期对象与 Mongoose 和 Nodejs 聚合

javascript - 如何在mongodb中通过纯js​​on中的Id查询文档

node.js - 聚合组上的 Mongoose 错误

mongodb - 在 mongodb 中展开空数组

mongodb - 为什么 mongodb 与 Casbah 存在多个连接?

mongodb - 如何编写自连接查询

javascript - Mongodb $in 针对数组对象字段而不是数组对象

mongodb - 投影嵌套数组中的特定元素

Mongodb concat int 和 string

mongodb - 过滤子文档数组