比如说有合集test如下共有2000条记录:
[
{
"_id": "1",
"myArray": [
{
"no": 1,
"status": 0,
"type": 1
},
{
"no": 2,
"status": 2,
"type": 1
},
{
"no": 3,
"status": 0,
"type": 2
},
{
"no": 4,
"status": 0,
"type": 1
}
]
},
{
"_id": "2",
"myArray": [
{
"no": 1,
"status": 0,
"type": 1
},
{
"no": 2,
"status": 2,
"type": 1
},
{
"no": 3,
"status": 0,
"type": 1
}
]
},
{
"_id": "3",
"myArray": [
{
"no": 1,
"status": 0,
"type": 2
},
{
"no": 2,
"status": 2,
"type": 1
},
{
"no": 3,
"status": 0,
"type": 1
}
]
},
...
{
"_id": "2000",
"myArray": [
{
"no": 1,
"status": 0,
"type": 1
},
{
"no": 2,
"status": 1,
"type": 1
},
{
"no": 3,
"status": 0,
"type": 2
}
]
}
]
需求是把合集里所有记录里data数组中type=1,status=0更新为status=4,请问如何实现?
试了.where().update()都无法实现
使用.aggregate()只能输出聚合结果无法更新内容
db.collection('test')
.aggregate()
.project({
myArray: $.filter({
input: '$myArray',
as: 'item',
cond: $.and([$.eq(['$$item.type',1]), $.eq(['$$item.status', 2])])
})
})
.end()
.then(res =>{
console.log(res)
})

有解决方案了吗?老铁。。。
可以试试先把这些符合条件的查询出来,知道数组下标以后,用拼接字符串的形式,再update回去
db.collection('chatroom').where({ myArray: _.elemMatch({ type: 1, status: 0, }) }).update({ data: { "myArray.$.status": 4, } })比如说
"type": 2},{"no": 4,"status": 2,"type": 1}]},{"_id": "2","myArray": [{"no": 1,"status": 1,"type": 1},{"no": 2,"status": 2,"type": 1},{"no": 3,"status": 0,"type": 1}]}]db.collection('chatroom').where({myArray: _.elemMatch({type: 1,status: 2,})}).update({data: {"myArray.$.status": 4,}})id:1记录myArray中no:2和no:4 id:2记录myArray中no:2是符合更新条件的.而执行这个update只能更新id:1记录了myArray中no:2和id:2记录myArray中no:2,id:1记录myArray中no:4这个数组不会更新{"_id": "1","myArray": [{"no": 1,"status": 1,"type": 1},{"no": 2,"status": 2,"type": 1},{"no": 3,"status": 0,"type": 2},{"no": 4,"status": 2,"type": 1}]},{"_id": "2","myArray": [{"no": 1,"status": 1,"type": 1},{"no": 2,"status": 2,"type": 1},{"no": 3,"status": 0,"type": 1}]},http://www.imooc.com/wenda/detail/558748