2

I want to create a query that will increment a counter in an object inside an array, but in case the object does not exist - it will be created (upsering)

example:

{
'_id' : 'someId',
someArray : [..,{'name':'a', 'count':234},..],
...
}

how will an upsert of the object {'name' : 'b'} will look like?

I'm using Node.js for the server side if that matter.

gmadar
  • 1,884
  • 3
  • 19
  • 22
  • My advice: change your database structure, i.e. make `someArray` a seperate collection with reference to your main collection. – freakish Apr 04 '13 at 07:52

1 Answers1

0

thanks @freakish.

I did changed my structure - but still stayed with one collection. instead of an array I used an object:

someObject : {'a':234, 'b':1}...

I did it this way because I understand there is a problem to upsert an array element with the $inc property: How to increment MongoDB document object fields inside an array

this solution works for me since I do not expect more than 100 elements in this object

Community
  • 1
  • 1
gmadar
  • 1,884
  • 3
  • 19
  • 22