Skip to content Skip to sidebar Skip to footer

Mongoose - Increment A Value Inside An Array Of Objects

I am having hard time figuring out how to increment a value in an object within an array For instance I have this document based on Poll schema: { '_id': '584b2cc6817758118e9557d

Solution 1:

It is not possible to directly increment the value in the .find query if labelOptions is an Array of Object. To make it easier, you should change the labelOptions type from Array of Objects to Object:

"labelOptions": {
    "Bob": 112,
    "Billy": 32,
    "Joe": 45
};

Also consider using .findByIdAndUpdate instead of .findOneAndUpdate if you are querying by the document's _id. And then, you can achieve what you want by:

Poll.findByIdAndUpdate(
    id,
    {$inc: {`labelOptions.${labelOption}`: 1 }},
    function(err, document) {
    console.log(err);
});

UPDATE: If you are persistent on using Array of Objects for labelOptions, there is a workaround:

Poll.findById(
    id,
    function (err, _poll) {

        /** Temporarily store labelOptions in a new variable because we cannot directly modify the document */
        let _updatedLabelOptions = _poll.labelOptions;

        /** We need to iterate over the labelOptions array to check where Bob is */
        _updatedLabelOptions.forEach(function (_label) {

            /** Iterate over key,value of the current object */for (let _name in _label) {

               /** Make sure that the object really has a property _name */if (_label.hasOwnProperty(_name)) {

                   /** If name matches the person we want to increment, update it's value */if (_name === labelOption) ++_label._name;
               }
           }
        });

        /** Update the documents labelOptions property with the temporary one we've created */
        _poll.update({labelOptions: _updatedLabelOptions}, function (err) {

            console.log(err);
        });
    });

Solution 2:

There is another way to do this which allows a more flexible document model. If you add a field to your object like:

{"_id":"584b2cc6817758118e9557d8","title":"Number of Skittles","description":"Test1","date":"Dec 9, 2016","__v":0,"labelOptions":[{"name":"Bob","number":112},{"name":"Billy","number":32},{"name":"Joe""number":45}]}

Then you can do:

app.put('/polls/:id', function(req, res){
  letid = req.params.id;
  let labelOption = req.query.labelOption;
  Poll.findOneAndUpdate(
    {
     '_id' :  id,
     'labelOptions.name
    },
    {$inc: {
      `labelOptions.$.number`: 1 
    }},
    function(err){
      console.log(err)
    })

Post a Comment for "Mongoose - Increment A Value Inside An Array Of Objects"