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

2 Answers 2

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())
Sign up to request clarification or add additional context in comments.

3 Comments

this will set when u know the index. I want to update dynamically
That's not quite what your question states "...want to update .. of index 1" :). So, how would you determine the index? By the clusterId?
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" } ] }
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*/
      
    }
  ]
})

7 Comments

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"}.
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"}
yes , I have gone through positional set and all but could not get the idea of writing query in java
Updated the query. Can run and check if that is what you expect.
Once we get the expected result. We can work on converting it to java
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.