简体   繁体   English

使用 $lookup 使用聚合管道更新文档

[英]Update document with aggregation pipeline with $lookup

I have a product collection which has fields -userId,referenceProductId, I want to add new field buyerUserId to all doc where its value will be equal to userId for documents where its _id is equal to referenceProduct_id For example-for following 2 doc我有一个产品集合,其中包含字段-userId,referenceProductId,我想将新字段buyerUserId添加到所有文档,其中它的值将等于userId,用于其_id等于referenceProduct_id的文档例如-对于以下2个文档

{
  "_id": { "$oid": "61ded34c1e7007b17a86f889" },
  
  "userId": { "$oid": "6190b06b113314ad2183db09" },
  
  "referenceProductId": { "$oid": "61ded15fdd1363aa1ce09c55" }
  
}

{
    "_id": { "$oid": "61ded15fdd1363aa1ce09c55" },
    
    "userId": { "$oid": "6190b06b113314ad2183db09" },
    
    "referenceProductId": { "$oid": "61ded34c1e7007b17a86f889" }
    
  }

BuyerUserId for doc1 will be 6190b06b113314ad2183db09 since doc1's _id is equal to referenceProductId of doc2 doc1 的 BuyerUserId 将为 6190b06b113314ad2183db09,因为 doc1 的 _id 等于 doc2 的 referenceProductId

I am new to mongoDB, trying to update with below code but doesn't work我是 mongoDB 的新手,尝试使用以下代码更新但不起作用

    { "$match": { "status": "purchased" }},
    {   $lookup:{
            from:"product",
            let:{
               "id":"$Id",
                "referenceProductId":"$ReferenceProductId",
                "userId":"$UserId",
            },
            pipeline:[
                {
                    $match:{
                        $expr:{
                             $eq: ["$$id", "$referenceProductId"] ,
                                
                        }
                    }
                },
            ],
            as:"products"
        }
    },{
        $project:{
             "buyerUserId":"$products.userId"
        }
    }
])

As noted in a comment above, $merge can be used as "doc to doc" update mechanism.如上面评论中所述, $merge可以用作“doc to doc”更新机制。 Starting in v4.4 you may output the results of the merge directly back onto the collection being aggregated.从 v4.4 开始,您可以 output 将合并的结果直接返回到正在聚合的集合中。

db.foo.aggregate([
    {$lookup: {from: "foo",
               let: {rid: "$_id"},
               pipeline: [
                   {$match:{$expr:{$eq: ["$$rid", "$referenceProductId"]}}}
               ],
               as: "Z"}}

    // If _id->refId match happened, set buyerUserId else do not set
    // anything not even null ($$REMOVE means "do nothing"):
    ,{$project: {buyerUserId: {$cond: [ {$ne:[0,{$size: "$Z"}]}, "$_id", "$$REMOVE"] }} }

    // At this point in pipeline we only have _id and maybe buyerUserId.  Use _id
    // from each doc to merge buyerUserId back into the main collection.
    // Since the docs came from this collection, we should fail if something
    // does not match.
    ,{$merge: {
        into: "foo",
        on: [ "_id" ],
        whenMatched: "merge",
        whenNotMatched: "fail"
    }}

]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM