0

I have a collection with such documents stuct:

{ 
  id: 1, 
  my_field: [
    { 
      date: date1, 
      type: A,
      value: 1
    }, 
    { 
      date: dateX, 
      type: X, 
      value: Z
    }, 
    ...
  ]

What the problem i solve: I have id, date, type and value. I have to find document by id(which is always present), then element in my_field by date and type and increment value. If this element doesnt present in my_field i have to insert it(struct to my_field). And i look for the most efficient way

I will be very thankful for ideas!

Mongo 4.2

Also it will be great if someone helps with to write this on ruby-driver for mongo :)

Thanks!

  • Here is a similar post: [mongoose update array or add to the array](https://stackoverflow.com/questions/61234012/mongoose-update-array-or-add-to-the-array). – prasad_ Apr 24 '20 at 03:12