如何在java中更新集合中特定索引处的对象数组列表 公共类客户{ 私人长id; 私有字符串名称; 私有字符串companyName; 私有列表环境=新建ArrayList(); } 公共课堂环境{ 私人内部集群; 私有字符串clusterName } 我在Mongo DB中的收藏 { “id”:1, “名称”:“xyz”, “公司名称”:“abc”, “环境”:[ { “集群式”:2, “clusterName”:“qwe” }, { “集群式”:6, “clusterName”:nme } ] } 要使用返回的整个环境列表更新索引1的环境列表。如何使用spring boot执行此操作?我要更新集合中获取其他列表的特定索引。 公共int updateEnv(客户){ 查询=新查询(); query.addCriteria(Criteria.where(“id”)是(customer.getid()); 更新=新更新(); update.set(“environment”,customer.getEnvironment()); 返回mongoUtil.update(查询、更新、Customer.class); }
此查询使用列表中的值而不是特定索引中的值更新整个环境列表。请指导位置运算符。如何使用java中的位置运算符执行此操作,您应该能够将索引传递给如何在java中更新集合中特定索引处的对象数组列表 公共类客户{ 私人长id; 私有字符串名称; 私有字符串companyName; 私有列表环境=新建ArrayList(); } 公共课堂环境{ 私人内部集群; 私有字符串clusterName } 我在Mongo DB中的收藏 { “id”:1, “名称”:“xyz”, “公司名称”:“abc”, “环境”:[ { “集群式”:2, “clusterName”:“qwe” }, { “集群式”:6, “clusterName”:nme } ] } 要使用返回的整个环境列表更新索引1的环境列表。如何使用spring boot执行此操作?我要更新集合中获取其他列表的特定索引。 公共int updateEnv(客户){ 查询=新查询(); query.addCriteria(Criteria.where(“id”)是(customer.getid()); 更新=新更新(); update.set(“environment”,customer.getEnvironment()); 返回mongoUtil.update(查询、更新、Customer.class); },java,spring-boot,Java,Spring Boot,此查询使用列表中的值而不是特定索引中的值更新整个环境列表。请指导位置运算符。如何使用java中的位置运算符执行此操作,您应该能够将索引传递给更新查询。因此,如果要更新/替换索引1中的元素,你可以做: public Class Customer{ private long id; private String name; private String companyName; private List<Environment> environment =
更新查询。因此,如果要更新/替换索引1
中的元素,你可以做:
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);
}
尝试此更新的查询
我正在尝试执行此操作,但它给了我一个错误在path'环境中找不到标识符'element'的数组筛选器。$[element].clusterId'
请帮助我。如何为ArrayFilter使用更新选项。这将在您知道索引时设置。我想动态更新这与您的问题所述不完全一致“…想更新。。那么,您将如何确定索引?通过clusterId
?假设在我的集合中我有一个基本上是arraylist的环境。现在我想运行查询来更新So,而不是{“id”:1,“name”:“xyz”,“companyName”:“abc”,“environment”:[{“clusterId”:2,“clusterName.”“:“qwe”},{“clusterName”:6,“clusterName”:nme“}”}`我想有`{“id”:1,“name”:“xyz”,“companyName”:“abc”,“environment”:[{“clusterid”:3,“clusterName”:“fgh”},{“clusterName”:nme“}我在上面给出了一个集合的例子,其中给出了环境列表,我想更新整个集合,例如,我想更新{“clusterid”:6,“clusterName”:nme“},而不是{“clusterid”:4,“clusterName”:“asd”}.好的,酷。这是我理解的。如果我错了,告诉我。那么我们有要更新的对象的信息对吗?就像在你的评论中,我们知道我们想用{“clusterid”:4,“clusterName”:“asd”}
是的,我已经研究了位置集,但没有想到用Javascript编写查询,而是更新了查询。可以运行并检查这是否是您所期望的。一旦我们得到预期的结果。我们可以把它转换成java
update.set("environment.1", customer.getEnvironment())
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*/
}
]
})
public int updateEnvironmentByCluster(long Id,int clusterId,String clusterName) {
Query query = new Query();
query.addCriteria(Criteria.where("Id").is(Id));
Update update = new Update();
update.set("environment.$[element].clusterId", clusterId);
update.set("environment.$[element].clusterName", clusterName);
UpdateOptions updateOptions = new UpdateOptions();
updateOptions.arrayFilters(Arrays.asList(Filters.in("element.clusterId", Arrays.asList(clusterId))));
System.out.println(query);
return mongoUtil.update(query, update,Customer.class);