如何在聚合管道mongodb中使用$ update / $ set运算符?

时间:2019-11-14 02:04:54

标签: mongodb mongodb-query aggregation-framework nosql-aggregation

我正在尝试根据某些条件更新我的name集合中名为coll1的字段。我首先创建了一个创建聚合管道,该管道根据我的标准过滤掉文档。

var local_filter = { "$or" :[ 
                                          {'fullText': {'$eq': "404 Error"}},
                                          {'fullText': {'$eq': "Unknown Error"}},
                                          {'fullText': {'$eq': "503 Error"}},
                                          {'fullText': {'$eq': "400 Error"}},
                                          {'fullText': {'$eq': "500 Error"}},
                                          {'fullText': {'$eq': "Read timed out"}},
                                          {'fullText': {'$eq': "410 Error"}},
                                          {'fullText': {'$eq': "403 Error"}},
                                          {"fullText": {'$eq':""}},
                              ]}

var foreign_filter= { "$and" :[
                              {'matchingrecords.Text': {'$ne': "404 Error"}},
                              {'matchingrecords.Text': {'$ne': "Unknown Error"}},
                              {'matchingrecords.Text': {'$ne': "503 Error"}},
                              {'matchingrecords.Text': {'$ne': "400 Error"}},
                              {'matchingrecords.Text': {'$ne': "500 Error"}},
                              {'matchingrecords.Text': {'$ne': "Read timed out"}},
                              {'matchingrecords.Text': {'$ne': "410 Error"}},
                              {'matchingrecords.Text': {'$ne': "403 Error"}},
                              {"matchingrecords.Text": {'$ne': ""}},
                              {"matchingrecords.Text": {'$ne':'null'}}
                              ]}

db.coll1.aggregate([
    {$match:local_filter //9474
    },
    {$lookup: {
           from: "coll2",
           localField: "_id",   //from coll1
           foreignField: "_id", //from coll2
           as: "matchingrecords"
         }//4518
    },
    { $match: foreign_filter
    },
    { $match: {matchingrecords: {$ne:[]} }
    },//3645
    {
      $count: "totalCount"
    }
    ])//3645

因此,我现在在coll1中获得3645个文档,我需要更新name字段。我尝试了2种方法,但两种方法都不起作用:

  1. { $set: { "Name" : matchingrecords.Text } }添加到上述管道中。这实际上将Name设置为字符串matchingrecords.Text,而不是字符串值。另外,添加$也无济于事!

  2. 使用aggregation with Update,在u子句中通过了聚合管道。

    db.runCommand(
              {
                update: "coll1",
                updates: [
                   {
                     q: { },
                     u: [// You can pass you aggregation pipeline here
                             {$match: local_filter//9474
                              },
                              {$lookup: {
                                     from: "coll2",
                                     localField: "_id",
                                     foreignField: "_id",
                                     as: "matchingrecords"
                                   }//4518
                              },
                              { $match: foreign_filter
                              },
                              { $match: {matchingrecords: {$ne:[]} }
                              },//3645
                              { $set: { "Name" : 'matchingrecords.Text' } }
                         ],
                         multi: true
                       }
                    ],
                    ordered: false,
                    writeConcern: { w: "majority", wtimeout: 5000 }
                 })

它抱怨$match operator isn't allowed in update!

{ 
    "n" : 0.0, 
    "nModified" : 0.0, 
    "writeErrors" : [
        {
            "index" : 0.0, 
            "code" : 72.0, 
            "errmsg" : "$match is not allowed to be used within an update"
        }
    ], 
    "ok" : 1.0
}

关于如何更新3645文档的任何建议?

有了一个简单的技巧!

解决方案(对我有用!):

  1. 使用coll1,用3645个文档创建一个新集合。
      db.coll1.aggregate([
        {$match:filter //9474
        },
        {$lookup: {
               from: "coll2",
               localField: "_id",
               foreignField: "_id",
               as: "matchingrecords"
             }//4518
        },
        { $match: foreign_filter
        },
        { $match: {matchingrecords: {$ne:[]} }
        },//3645
        { $unwind: { path: "$matchingrecords", preserveNullAndEmptyArrays: true }
        },
        { $project : <what All fields you Need?>
        },
        { $out: "child_coll1"
        }//get 3645 in the a new collection
  1. 使用coll1,在单独的集合中获取不匹配的文档
   db.coll1.aggregate([
        {$lookup: {
               from: "child_coll1",
               localField: "_id",
               foreignField: "_id",
               as: "matchingrecords"
             }//
        },
        { $match: {matchingrecords: {$eq:[]} }
        },//30944
        { $unwind: { path: "$matchingrecords", preserveNullAndEmptyArrays: true }
        },
        { $out: "child_coll2"
        }//get out 30944 docs other than 3645
    ])
  1. 只需合并1和2中的新收藏
        db.child_coll1.find().forEach(function(doc){
           db.child_coll2.insert(doc); 
        });
  1. 删除child_coll2以外的所有集合,您可以将其重命名为coll1

这不是一个优雅的解决方案,只是一个可以完成任务的技巧!有人在一个查询中有更好/优雅的解决方案吗?

1 个答案:

答案 0 :(得分:0)

为什么您的第一个聚合解决方案不起作用?

$set不是用于聚合管道的有效阶段运算符。

为什么第二个带有update命令的解决方案不起作用?

Update命令不接受聚合管道运算符。根据{prasad_共享的reference,仅支持mongo@4.2开头的几个操作符

那么如何解决此问题?

检查是否可以将$replaceRoot用于您的用例。如果没有,您可以使用以下技巧。

首先像您一样从聚合开始,然后添加$addFields阶段以为每个文档添加一个具有您要设置的值的新字段。然后按如下所示运行另一个更新命令

db.coll1.aggregate([
   { 
   // your match queries 
   },
   {
   $addField: { myNewName: "myvalue" } 
   }
]).toArray().forEach(function(myDoc){
   db.coll1.update({ _id: myDoc.id }, { $set: {Name: myDoc.myNewName } })
})