0

If I have a collection mycoll of documents like this :

{
    vals : [10, 11]
}

where the length of the vals array is NOT fixed.

is there a way to $inc ALL the elements in the vals array with one command ?

I've tried

db.mycoll.update({}, { $inc : { vals : 5 }}, { multi : true })
// error cannot $inc non-number

db.mycoll.update({}, { $inc : { vals.$ : 5 }}, { multi : true })
// error must specify a field

and the goal is to get

{
    vals : [15, 16]
}

without having to fetch each document, copy and update the array, then save the updated array back to the document...

my thanks in advance for any ideas !

Petrov
  • 4,200
  • 6
  • 40
  • 61

2 Answers2

0

whoops, there is already a JIRA open for this here

https://jira.mongodb.org/browse/SERVER-1243

Petrov
  • 4,200
  • 6
  • 40
  • 61
0

You can include multiple fields in your $inc, so while you need to explicitly reference each element by its index, you can do this as:

db.mycoll.update({}, {$inc: {'vals.0': 5, 'vals.1': 5}})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • thanks for the idea ! but the length of the arrays are not fixed unfortunately.. i've updated the question – Petrov Jan 25 '15 at 15:15