$合并仅保存一个文档,而不是所有获取的文档:mongodb

问题描述 投票:0回答:1

这是我的查询

 db.orders.aggregate([
                {
                '$match': {
                         'date': ISODate("2020-02-11T18:30:00Z")
                }},
                {
                '$unwind': {
                        'path': '$orders',
                 }}, 
                {
                 $lookup:
                   {
                        from: 'retailprice',
                        let: { productCode: '$orders.productCode' },
                        pipeline: [
                        {
                                $match: {
                                frequency: 'FR',
                                $expr: {
                                        $eq: ["$productCode", "$$productCode"]
                                     }
                                  }
                        }
                        ], as: 'retailprices'
                    }
                },
                { '$unwind': '$retailprice' },
              {
                $lookup:
                     {
                        from: 'discounts',
                        let: { productCode: '$orders.productCode' },
                        pipeline: [
                        {
                                $match: {
                                frequency: 'FR',
                                $expr: {
                                        $eq: ["$productCode", "$$productCode"]
                                }
                             }
                        }
                        ], as: 'discounts'
                }},
                { '$unwind': '$discounts' },
                {
                        '$project':{
                        'productCode':'$orders.productCode' ,
                        'vendorId':'$vendorId',
                        'dealerId':'$dealerId',
                        'quantity':'$orders.quantity'
                        } 
                },{'$merge':'test'}
        ]).pretty()

因此,此查询的不带{'$merge':'test'}的输出为:

 {
    "_id" : ObjectId("5e3a710af2657521e8c5668a"),
    "productCode" : "TCE1",
    "vendorId" : ObjectId("5e3d05ba964d0e06c4bb0f07"),
    "dealerId" : ObjectId("5e1d82156a67173cb877f67d"),
    "quantity" : 30,
   }
 {
    "_id" : ObjectId("5e3a710af2657521e8c5668a"),
    "productCode" : "ECE1",
    "vendorId" : ObjectId("5e3d05ba964d0e06c4bb0f07"),
    "dealerId" : ObjectId("5e1d82156a67173cb877f67d"),
    "quantity" : 67,
   }

但是当我使用{'$merge':'test'}将输出保存到新集合中时,它仅保存了一个文档,即:

{
    "_id" : ObjectId("5e3a710af2657521e8c5668a"),
    "dealerId" : ObjectId("5e1d82156a67173cb877f67d"),
    "productCode" : "ECE1",
    "quantity" : 67,
    "vendorId" : ObjectId("5e3d05ba964d0e06c4bb0f07")
 }

现在为什么?我认为这是因为如果检查了查询输出,两个文档中的"_id":ObjectId("5e3a710af2657521e8c5668a")是相同的,如果是这样的话,可以如何解决。如何使用$merge保存两个文档,我不想使用$out,因为无论何时再次运行查询,它都将替换整个文档,或者我该如何将所有提取的文档保存到新集合中

node.js mongodb aggregation-framework
1个回答
0
投票
[考虑到_id问题时,您假设是正确的:$ unwind阶段为每个数组元素生成一个文档,所有文档都具有相同的_id(与其他字段一样,是从原始文档复制的)诀窍是...非常简单!在您的$ project阶段添加“ _id”:0。然后,mongodb将在$ merge插入上create a new _id

... { '$project':{ '_id':0, 'productCode':'$orders.productCode' , 'vendorId':'$vendorId', 'dealerId':'$dealerId', 'quantity':'$orders.quantity' } }, ...

但是请注意,通过这种方式,mongoDB将

allways创建新的插入,而从不更新现有的插入。如果需要更精确的行为,请检查$merge可选属性

© www.soinside.com 2019 - 2024. All rights reserved.