如何从吗啡中的 ArrayList 中删除特定元素?
How to remove a specific element from ArrayList in morphia?
我在 Morphia Entity
class.
中使用继承的 @Embedded
ArrayList 引用
@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) 如何删除给定值为titleSecond
和id
的元素?
2) 如何删除给定值为titleThird
和id
的元素?
首先我尝试了以下实现:
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。其余 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);
我在 Morphia Entity
class.
@Embedded
ArrayList 引用
@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) 如何删除给定值为titleSecond
和id
的元素?
2) 如何删除给定值为titleThird
和id
的元素?
首先我尝试了以下实现:
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。其余 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);