假设文档如下
{
id:7778888
itemList:[{type:1,count:10},{type:2,count:12},{type:21,count:21}],
nick:"hehe"
}
有两个问题:
1.现在要向该文档的itemList添加元素{type:90,count:23} 如果itemList中已经有元素的type为90则直接将该元素的count加23,如果没有则直接将该元素添加到itemList中,要怎么用一条更新实现这个功能呢?
2.假设该文档的itemList中没有type为90的元素,现在向itemList中添加元素{type:90,count:23}, 同时更新type为2的元素的count减10,我是这样做的,但是报错了:
testmodel.update(
{id:7778888},
{
$inc:{"itemList.$[elem].count":-10},
$push:{itemList:{type:90,count:23}}},
{arrayFilters:[{"elem.type":2}]}
);
mongoError: Updating the path 'itemList.$[elem].count' would create a conflict at 'itemList'
有人知道这是为啥吗?