首页 文章

在尝试使用聚合查找和更新时,获取错误“管道元素3不是对象错误”

提问于
浏览
2

我正在使用 node js mongodb driver 并尝试更新文档中对象数组内的 object array .

文档集的 schema 是这样的:

enter image description here

What I Want :

对于 order no = 1 & items.qty=2 & tax rate = 25 的集合,请更新 tax to "cst" & taxratetype to "flat" .

What I Tried :

db.OrderInfo.aggregate(
    {$match:{"orderno":"1"}},
    {$unwind:'$items'},
    { $match: { 'items.qty' : 2} 
 },function(err,result1){
    if(err){
    throw(err);
   }else{
   indexes = result1[0].items.taxes.map(function(obj, index) {
      if(obj.taxrate == 25) {
      return index;
   }
   }).filter(isFinite);

var updateData = {};
updateData["items.$.taxes."+indexes[0]+".tax"]="cst";
updateData["items.$.taxes."+indexes[0]+".taxratetype"]="flat";


db.OrderInfo.update({ "orderno":"1",'items.qty': 2,'items.taxes.taxrate': 25 },{$set: updateData },function(err,result2){
console.log(result2);
});

}
});

目前我正在使用 db.eval 从节点运行此脚本,但稍后我会在完成相同操作后更改它 .

Getting this Error :

{“name”:“MongoError”,“message”:“错误:命令失败:{\ n \ t \”ok \“:0,\ n \ t \”errmsg \“:\”管道元素3不是对象\“,\ n \ t \”代码\“:15942 \ n}:聚合失败:\ n_getErrorWithCode@src/mongo/shell/utils.js:25:13 \ ndoassert @src / mongo / shell / assert . JS:13:14个\ nassert.commandWorked@src/mongo/shell/assert.js:267:5个\ nDBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5 \ n_funcs1 @:1:31 \ n“,”ok“:0,”errmsg“:”错误:命令失败:{\ n \ t \“ok \”:0,\ n \ t \“errmsg \”:\“管道元素3不是对象\“,\ n \ t \”代码\“:15942 \ n}:聚合失败:\ n_getErrorWithCode@src/mongo/shell/utils.js:25:13 \ ndoassert @src / mongo / shell / assert . JS:13:14个\ nassert.commandWorked@src/mongo/shell/assert.js:267:5个\ nDBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5 \ n_funcs1 @:1:31 \ n”, “代码”:139}

我从这个问题https://jira.mongodb.org/browse/SERVER-831知道我不能使用直接更新命令,因此尝试这种解决方法 . 这种更新的任何其他方法对我来说也没问题 .

EDIT : 根据@ titi23给出的答案,我曾尝试在函数内部使用[] . 它没有给我任何错误,但我的 Value 观也没有得到更新 .

1 回答

  • 2

    查询中的两个问题:

    1)您在 aggregate 查询中缺少 [] .

    2)更新方法不需要税率条款 . 它会发现嵌套文档和聚合索引在更新时可以达到目的 .

    有关如何使用它的更多信息,请参阅aggregate-definition .

    语法 - db.collection.aggregate(pipeline,options)pipeline - array - 一系列数据聚合操作或阶段 .

    请尝试以下方法: -

    db.OrderInfo.aggregate([
    {$match:{"orderno":"1"}},
    {$unwind:'$items'},
    { $match: { 'items.qty' : 2} }]).toArray(
    function(err,result1){
    if(err){
        throw(err);
    }
    else{
       console.log(result[0]); //See is there any record here
       indexes = result1[0].items.taxes.map(function(obj, index) {
      if(obj.taxrate == 25) {
      return index;
      }
     }).filter(isFinite);
    
    var updateData = {};
    updateData["items.$.taxes."+indexes[0]+".tax"]="cst";
    updateData["items.$.taxes."+indexes[0]+".taxratetype"]="flat";
    
    
     db.OrderInfo.update({ "orderno":"1",'items.qty': 2}, /*Remove the tax rate clause from here..*/
          {$set: updateData },function(err,result2){
            console.log(result2);
        });
      }
    });
    

    它不应该抛出错误 .

    EDIT:- 用聚合做 toArray() ,看看是否有帮助 . 已经更新了查询 .

相关问题