【问题标题】:How to remove a specific element from ArrayList in morphia?如何从morphia中的ArrayList中删除特定元素?
【发布时间】:2016-12-24 02:12:50
【问题描述】:

我在 Morphia Entity 类中使用继承的 @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) 如何删除给定值为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

【问题讨论】:

  • 只是好奇您最初是如何创建此文档的?你能把记录推到三等吗?单独的问题。二等如何与三等联系在一起?三等舱可以和二等舱一样移动吗?
  • 只是好奇你最初是如何创建这个文档的 我传递了上面的JSON 对象并使用datastore.save(firstObject) 保存。 *你能把记录推到三等吗*:是的,我可以看到MongoDB中的记录。 *二等与三等的关系如何*:我在Second Class中使用了@Embedded private List&lt;Third&gt; thirdClass;。请查看已发布的 JSON (` "thirdClass": [{...}]`)。 *你能把三等班和二等班一样移动吗*实际上,我不能:(。如果我已经解决了你所有的问题,请告诉我?
  • 其实不是第一次保存。我的问题是,如果您想在现有对象的第三类中添加新条目怎么办?这是你尝试过的吗?
  • 还没有,我必须这样做。如果我运行 update the existing in secondClass 会不会是类似的情况?让我先尝试实施您的解决方案。
  • 好的。我认为它不一样。至少我在尝试删除三等舱的条目时无法解决这个问题。我会让你试着挤进三等舱。这部分我没试过。

标签: java mongodb playframework morphia


【解决方案1】:

这样创建记录。

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"
}])

我的第一堂课。其余类相同。

@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);

【讨论】:

  • 我试过了,但什么也没发生。无一例外都得到了成功的回应。我检查了数据库,但条目在那里。你想让我找到TitleSecond,获取它并传递给removeAll("secondClass", second吗?或者我只需要复制你的代码?
  • 添加了完整的代码。它将帮助您更好地理解。也添加了答案的第二部分。我正在使用 Morphia 1.3.0
  • 感谢 sagar,“第一部分的替代解决方案”和“第二部分的答案”标题对我有用。 :)
猜你喜欢
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2012-02-07
  • 1970-01-01
  • 2018-11-05
相关资源
最近更新 更多