addToSet in array via Spring Boot Mongo template

135 views Asked by At

FirstDoc:

{
  "_id": "containerId1",
  "executionID" : "executionId1",
  "assignedContainers": [ {
      "_id":"container2", 
      "manufacturingItemGroups":[
        {
          "_id": "figure2",
          "amount": 2
        }
      ],
   }],
  "manufacturingItemGroups": [
    {
      "_id": "figure1",
      "amount": 1
    }
  ],
  "_class": "Figure"
}

SecondDoc:

{
  "_id": "containerId2",
  "executionID" : "executionId1",
  "assignedContainers": [ {
      "_id":"container1", 
      "figures":[
        {
          "_id": "figure1",
          "amount": 1
        }
      ],
   }],
  "figures": [
    {
      "_id": "figure2",
      "amount": 2
    }
  ],
  "_class": "Figure"
}

I want to use this to update my docs:

mongo.findAndModify(query, update, Figure.class);

I want to add an figure to my ContainerId1. So I also need to update the second doc => assignedContainers. But how do I do it?

I found something like:

update2.addToSet("assignedContainers.$.figures", new Figure("id3", 3));

But what does $ mean and how do I for example change the amount in all documents if it does change?

0

There are 0 answers