1

options.votes is an array.

This code works:

Poll.findOneAndUpdate({title},{$inc:{'options.votes.0':1}}

I need to replace 0 for a variable.

This does not work:

const query = 'options.votes.'+variable
Poll.findOneAndUpdate({title},{$inc:{query:1}}

Thanks for help

1 Answers1

3

It's just an object with a string key

var obj = {};

obj['options.votes.'+variable] = 1;

Poll.findOneAndUpdate({title},{$inc:obj})

or with ES2015 and dynamic keys

const query = 'options.votes.'+variable;
Poll.findOneAndUpdate({title},{$inc:{[query]:1}})
adeneo
  • 312,895
  • 29
  • 395
  • 388