[Mongoose] The save method reported an error.

  mongodb, question

The data are as follows:

{
 array: [
 {
 _id: 'aaa'
 sub_array: [
 {
 _id: 'saaa',
 name: 'aaa'
 },
 {...}
 ]
 },
 {...}
 ]
 }

Now I want to updatesub_arrayThenameValue:

Model.findOne({
 'array._id': 'aaa'
 },{'array.$': 1}).then(respon=> {
 let checkdt = respon.array[0].sub_array.find((item)=> {
 reruen item._id == 'saaa'
 })
 checkdt.name = 'new val'
 respon.save().then(r=> res.send(r)).catch(er=> res.send(er))
 })

The result is wrong:

{
 "message": "For your own good, using `document.save()` to update an array which was selected using an $elemMatch projection OR populated using skip,   limit, query conditions,   or exclusion of the _id field when the operation results in a $pop or $set of the entire array is not supported. The following path(s)   would have been modified unsafely:\n  questions.0.sub_questions.1.score\n  questions.0.sub_questions.1.title\nUse Model.update() to update these arrays instead.",
 "name": "DivergentArrayError"
 }

Why is this happening? How can I modify it?

Search for someone actually asked:https://stackoverflow.com/que …
Mongoose’s github also describes this problem:issues#1334
In short, you usefindOneWhen using the projection{'array.$': 1}Therefore, only a part of the original document is returned. AndsaveThe method is to save the document you gave back as it is, which is obviously not the result you want, because the data will be lost. For example:

rs0:PRIMARY> db.test.insert({a: 1, b: 2})
 WriteResult({ "nInserted" : 1 })
 rs0:PRIMARY> db.test.find()
 { "_id" : ObjectId("5a741072979a928dbe90341a"), "a" : 1, "b" : 2 }
 rs0:PRIMARY> var doc = db.test.findOne({}, {a: 1})
 rs0:PRIMARY> db.test.save(doc)
 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 rs0:PRIMARY> db.test.find()
 { "_id" : ObjectId("5a741072979a928dbe90341a"), "a" : 1 }

To put it bluntly, mongoose is trying to prevent you from hurting yourself, but it is still very moral and the conscience of the industry.