使用数组过滤器无法更新多层嵌套的mongo文档

时间:2019-09-30 09:14:56

标签: mongodb

我有一个具有以下架构的mongo文档

  {
    "_id" : ObjectId("5d78bbd1de3c1725e0df6bb3"), 
    "CategoryName" : "ABC",
    "DivisionDetails" : [ 
        {        
            "_id" : ObjectId("5d78bbd1de3c1725e0df6bb7"),
            "DivisionID" : "1",
            "DivisionQuesDetails" : [ 
                {

                    "_id" : ObjectId("5d78bbd1de3c1725e0df6bb9"),
                    "QuestionID" : "1",
                    "Question" : "abc"
                }, 
                {

                    "_id" : ObjectId("5d78bbd1de3c1725e0df6bb8"),
                   "QuestionID" : "2",
                    "Question" : "abcd"
                }
            ]
        }, 
        { 
            "_id" : ObjectId("5d78bbd1de3c1725e0df6bb4"),
            "DivisionID" : "2",
            "DivisionQuesDetails" : [ 
                {   
                    "_id" : ObjectId("5d90e9fe04ab909e6b8a4aea"),
                    "QuestionID" : "3",
                    "Question" : "Is it"
                }, 
                {
                     "QuestionID" : "4",
                    "_id" : ObjectId("5d90e9fe04ab909e6b8a4ae9"),
                    "Question" : "Isnt it"
                }
            ]
        }
    ],
    "__v" : 0
}

我想使用猫鼬在case(“ _ id”:ObjectId(“ 5d90e9fe04ab909e6b8a4ae9”))中更新问题。 下面是相同的代码

const query = {"_id": mongoose.Types.ObjectId("5d78b8910f59f825586c69c4")};
const updateObject = { 
  $set: {
    "DivisionDetails.$['element'].DivisionQuesDetails.[j].Question":
      "updatedQuestion"
  }
};
const filterValues = { 
  arrayFilters: [
    {"element._id" :mongoose.Types.ObjectId("5d78bbd1de3c1725e0df6bb4") }, 
    {"j._id" : mongoose.Types.ObjectId('5d90e9fe04ab909e6b8a4ae9')}
  ]
};

Model.findOneAndUpdate(query,updateObject,filterValues)

我总是收到错误消息

  

“在路径中找不到标识符'element'的数组过滤器”

TIA

0 个答案:

没有答案