gpt4 book ai didi

java - 如何从吗啡中的 ArrayList 中删除特定元素?

转载 作者:可可西里 更新时间:2023-11-01 10:18:52 27 4
gpt4 key购买 nike

我在 Morphia Entity 类中使用 ArrayList 的继承 @Embedded 引用。

@Entity
public class First {
@Embedded private List<Second> secondClass;
private String title;
private Long id;
... getter and setter .. methods
}

@Embedded
public class Second {
@Embedded private List<Third> thirdClass;
private String titleSecond;
... getter and setter .. methods
}

@Embedded
public class Third {
private String titleThird;
private String logoUrl
... getter and setter .. methods
}

JSON

{
"secondClass": [{
"thirdClass": [{
"titleThird": "Java",
"logoUrl": "http://www.artsfon.com/pic/201510//artsfon.com-72885.jpg",
}, {
"titleThird": "ios",
"logoUrl": "http://www.artsfon.com/pic//1920x1080/artsfon.com-72885.jpg",
}],
"titleSecond": "Developer"
}],
"title": "Software Developer",
"id" : 1234567890
}

问题

1) 如何删除给定titleSecondid值的元素?

2) 如何删除给定titleThirdid值的元素?

首先我尝试了以下实现:

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).filter("id", Long.parseLong(id.trim()));

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.titleSecond", "Developer");
datastore.update(updateQuery, ops);

但它抛出映射错误:

Write failed with error code 16837 and error message 'cannot use the part (secondClass of secondClass.titleSecond) to traverse the element.

如何执行问题 1 和 2 的操作?任何帮助将不胜感激。

可能相关的问题:In Morphia how can i update one embedded Object inside an ArrayList

最佳答案

创造了这样的记录。

db.myCollection.insertMany([{
"_id" : ObjectId("58609ed483ce6037ec33fc8c"),
"secondClass" : [
{
"thirdClass" : [
{
"titleThird" : "Java",
"logoUrl" : "http://www.artsfon.com/pic"
},
{
"titleThird" : "ios",
"logoUrl" : "http://www.artsfon.com/pic"
}
],
"titleSecond" : "Developer"
}
],
"title" : "Software Developer"
}])

我的第一个类。其余类(class)相同。

@Entity("myCollection")
public class First {

@Id
private ObjectId id;
private String title;
@Embedded
private List<Second> secondClass;

第一部分的答案。

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c"));

Second second = new Second();
second.setTitleSecond("Developer");
ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", second);
datastore.update(updateQuery, ops);

第一部分的替代解决方案。

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c"));

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", new BasicDBObject("titleSecond", "Developer"));
datastore.update(updateQuery, ops);

第二部分的答案。

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c")).field("secondClass.thirdClass.titleThird").equal("ios");

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.$.thirdClass", new BasicDBObject("titleThird", "ios"));
datastore.update(updateQuery, ops);

关于java - 如何从吗啡中的 ArrayList 中删除特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309729/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com