使用mongoose 查询数据列表后修改数据不成功
原因是mongoose查出来的数据表明是单层对象数据,实际它是一个多层对象数组
[{
'$__': InternalCache {
activePaths: [ctor],
skipId: true,
validationError: [Error]
},
'$isNew': false,
_doc: {
key: 'zhangsan'
},
'$errors': { _id: [ValidatorError] },
key2: 'xinzeng'
},
{
'$__': InternalCache {
activePaths: [ctor],
skipId: true,
validationError: [Error]
},
'$isNew': false,
_doc: {
key: 'lisi'
},
{
'$__': InternalCache {
activePaths: [ctor],
skipId: true,
validationError: [Error]
},
'$isNew': false,
_doc: {
key: 'wangwu'
},
'$errors': { _id: [ValidatorError] },
key2: 'xinzeng'
}
]
所以想要更改数据,必须更改_doc对象里面的数据,
var result = userCasterList.map((item) => {
var obj= {...item._doc};
obj.age = 18;
return obj;
})
console.log(result);//[{key:'zhangsan',age:18},{key:'lisi',age:18},{key:'wangwu',age:18}]