Mongo DB:如何从一个集合中复制文档并将其作为字段添加到另一个集合中的相关文档中? [英] Mongo DB: How to copy the Document from one collection and add it as a field to a related document from another collection?

查看:70
本文介绍了Mongo DB:如何从一个集合中复制文档并将其作为字段添加到另一个集合中的相关文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库中有2个收藏夹:

2 Collections are available in the DB:

产品:

{
   "_id":"unique_product_id",
   "name":"Product 1",
   "price":50
}

事件:

{
   "_id":"unique_event_id_1",
   "product_id":"unique_product_id",
   "action":"created"
},
{
   "_id":"unique_event_id_2",
   "product_id":"unique_product_id",
   "action":"updated"
}

其中 Events 集合包含与 Products 集合中的文档相关联的文档.我的想法只是将文档从 Events 集合注入到 Products ,结果, Products 将看起来像:

where Events collection contains documents associated with documents from Products collection. The idea that I have is just to inject documents from Events collection to Products, as results the Products will look like:

产品:

{
   "_id":"unique_product_id",
   "name":"Product 1",
   "price":50,
   "events":[
      {
         "_id":"unique_event_id",
         "product_id":"unique_product_id_1",
         "action":"created"
      },
      {
         "_id":"unique_event_id",
         "product_id":"unique_product_id_1",
         "action":"updated"
      }
   ]
}

我想了解是否有可能将其实现为 Mongodb Aggregation框架的一部分?

And I want to understand if it possible to implement it as part of the Mongodb Aggregation framework?

我需要找到类似 $ out 之类的东西,这些东西可以将聚合管道返回的文档写入到指定的集合中,但是我需要写的不是特定的集合,而是特定的集合特定收藏的文件.

I need to find something like $out that takes the documents returned by the aggregation pipeline and writes them to a specified collection, but I need to write not to a specific collection, but to the specific document of the specific collection.

推荐答案

从MongoDB 4.4开始,

Starting in MongoDB 4.4, $merge can output to the same collection that is being aggregated:

db.products.aggregate([       
   { /**
    * from: The target collection.
    * localField: The local join field.
    * foreignField: The target join field.
    * as: The name for the results.
    * pipeline: The pipeline to run on the joined collection.
    * let: Optional variables to use in the pipeline field stages.
    */
   $lookup: {
     from: 'events',
     localField: '_id',
     foreignField: 'product_id',
     as: 'events'
   }},
   {/**
    * into: The target collection.
    * on: Fields to  identify.
    * whenMatched: Action for matching docs.
    * whenNotMatched: Action for non-matching docs.
    */
   $merge: {
     into: 'products',
     on: "_id",
     whenMatched: 'merge',
     whenNotMatched: 'insert'
   }}
])

请注意::当$ merge输出到正在汇总的同一集合时,文档可能会多次更新,或者该操作可能会导致无限循环.此处的更多详细信息 https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#merge-behavior-same-collection

Be aware: when $merge outputs to the same collection that is being aggregated, documents may get updated multiple times or the operation may result in an infinite loop. More details here https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#merge-behavior-same-collection

如果是一次性更新,则可以通过在初始阶段添加初始过滤器来确保文档精确地更新一次,从而保护管道:

If it is a one off update you can safeguard the pipeline by adding initial filter as the first stage to ensure a document is updated exactly once:

{ $match: { events: { $exists: false } }

这篇关于Mongo DB:如何从一个集合中复制文档并将其作为字段添加到另一个集合中的相关文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆