我想更新MongoDB中元素的值。元素的类型为array,并将数组的值作为键值对传递。
名称是唯一的,我想更新名称匹配的state_history
{
state_history: [],
_id: 5c4955c4d3804953605a9dca,
name: 'aaa',
imageName: '11-soyabean-868187035471289-1548149389176.jpg',
createdAt: 2019-01-24T06:05:56.640Z,
updatedAt: 2019-06-07T12:05:40.338Z
}
function insertUpdateState(name, row) {
db.update(
{ name: "aaa" },
{ $set: { row } }
)
}
let row = { "state_history.0":{"collection":"value1"},"state_history.1":{"processing":new Date()} }
insertUpdateState("aaa",row)
预期结果:-
state_history = [
0 : {collection:value1}
1 : {processing:2019-01-24T06:05:56.640Z}
]
当前结果:-
state_history = []