0
public Class Customer{
    private long id;
    private String name;
    private String companyName;
    private List<Environment> environment = new ArrayList<>();
}

public Class Environment{
    private int clusterid;
    private string clusterName
}

My Collection in Mongo DB


{  
  "id":1,
  "name":"xyz",
  "companyName":"abc",
  "environment":[
    {
      "clusterid":2,
      "clusterName":"qwe"
    },
    {
      "clusterid":6,
      "clusterName":nme"
    }
  ]
}

 want to update environment List of index 1 with returning whole environment List. How do I do this with spring boot? I want to update particular index of list fetching other too in the collection .

public int updateEnv(Customer customer) {
    Query query = new Query();
    query.addCriteria(Criteria.where("id").is(customer.getid()));
    Update update = new Update();
    update.set("environment", customer.getEnvironment());
    return mongoUtil.update(query, update, Customer.class);
}

This query updating the whole environment List with the value in the list instead of at particular index. Please guide about positional operator .How to do this with positional operator in java

Asha SIngh
  • 39
  • 6

2 Answers2

0

You should be able to just pass the index to the update query. So if you want to update/replace the element at index 1, you can do:

update.set("environment.1", customer.getEnvironment())
eol
  • 23,236
  • 5
  • 46
  • 64
  • this will set when u know the index. I want to update dynamically – Asha SIngh Oct 24 '20 at 15:05
  • That's not quite what your question states "...want to update .. of index 1" :). So, how would you determine the index? By the `clusterId`? – eol Oct 24 '20 at 15:26
  • suppose in my collection I have environment which is basically an arraylist. now i want to run query for update so `instead` of { "id":1, "name":"xyz", "companyName":"abc", "environment":[ { "clusterid":2, "clusterName":"qwe" }, { "clusterid":6, "clusterName":nme" } ] } ` I want to have` { "id":1, "name":"xyz", "companyName":"abc", "environment":[ { "clusterid":3, "clusterName":"fgh" }, { "clusterid":6, "clusterName":nme" } ] } – Asha SIngh Oct 24 '20 at 17:19
0

Try this updated query.

Updated Mongo Playground

db.collection.update({
  id: 1/**customer id*/
  
},
{
  "$set": {
    "environment.$[element]": {
      "clusterId": 4,
      /**environment object with updated clusterId*/
      "clusterName": "asd"/**environment object with updated clusterName*/
      
    }
  }
},
{
  arrayFilters: [
    {
      "element.clusterid": 6/**clusterId of environment you have to update*/
      
    }
  ]
})
wak786
  • 1,562
  • 1
  • 8
  • 12
  • I have given a example of collection above in which environment List is given i want to update whole like for example instead of { "clusterid":6, "clusterName":nme" } , I want to update {"clusterid":4,"clusterName":"asd"}. – Asha SIngh Oct 24 '20 at 17:15
  • Okay cool. Here is what i understood. Tell me if am wrong. SO we have the information of which object to update right? Like in your comment we know that we want to update ` { "clusterid":6, "clusterName":nme" }` with `{"clusterid":4,"clusterName":"asd"}` – wak786 Oct 24 '20 at 18:10
  • yes , I have gone through positional set and all but could not get the idea of writing query in java – Asha SIngh Oct 24 '20 at 18:15
  • Updated the query. Can run and check if that is what you expect. – wak786 Oct 24 '20 at 18:16
  • Once we get the expected result. We can work on converting it to java – wak786 Oct 24 '20 at 18:18
  • yes that's the result I want .but how to write query in spring boot – Asha SIngh Oct 24 '20 at 18:43
  • you can use MongoTemplate. Here is a link which can be helpful. https://stackoverflow.com/questions/49906270/how-to-apply-update-using-filtered-positional-operator-with-arrayfilters – wak786 Oct 24 '20 at 18:49