Meteor mongodb $inc with update

前端 未结 2 466
清酒与你
清酒与你 2021-01-26 15:57

I have this mongo collection and vars:

items:{
 type_one: 0,
 type_two: 0
}

var valueOne = 1;
var nameItem = type_one;

I try to update a

相关标签:
2条回答
  • 2021-01-26 16:22

    You need to build your query dynamically using the bracket [] operator. Also "nameItem" must be a string.

    var valueOne = 1;
    var nameItem = 'type_one';
    var inc = {};
    inc[ 'items.' + nameItem ] = valueOne;
    Collection.update({ createdBy: user_id }, { '$inc': inc } )
    
    0 讨论(0)
  • 2021-01-26 16:33

    The only possible reasons I can come up with this that either you inserted number into collection in the form of String or you're trying to increment with String value. Try using parseInt(stringValue) in your code.

    0 讨论(0)
提交回复
热议问题