我有一个数据结构,就像这样
{
_id: '',
name: 'A',
financialReports: {
monthlyBill: 20,
monthlyBillReports: [
{
month: 'JAN',
isPaid: true
},
{
month: 'FEB',
isPaid: false
},
{
month: 'MAR',
isPaid: false
},
{
month: 'APR',
isPaid: false
},
...
]
}
},
...
如何动态将financialReports.monthlyBillReports[i].isPaid
更新为true?我从前端['FEB', 'APR']
获得了指示数据库中月份字段的数据数组,但是我不确定是否可以遍历该数组并对数据库进行更新查询。
答案 0 :(得分:1)
您可以执行如下循环:
this.posts.findById(UUID.fromString(id))
.subscribe()
.with(
post -> rc.response().end(toJson(post)),
throwable -> rc.fail(404, throwable)
);
或更直接的方式:
let yourArray = ['FEB', 'APR'] // just mocking the array
yourArray.forEach((mo)=>{
collectionName.findOneAndUpdate(
{_id: 'document id goes here'},
{ '$set': {'financialReports.monthlyBillReports.$[frFilter].isPaid': true}},
{ arrayFilters: [ {frFilter.month: mo} ], multi: true}
).then(
//do whatever you need here
)
})