在Morphia中,我如何更新ArrayList中的一个嵌入对象

时间:2012-02-08 23:23:26

标签: java mongodb morphia

将Mongodb与Morphia一起使用真的很新,并看到许多高级答案如何做到这一点。

如果可能的话,我想做到这一点,并且我有一个名为
@Embedded对象 fileObjects包含Files个对象。

我无法更新Files内的字段。

我想只更新String fileHash

中的一个字段
@Entity
public class BatchData {

    @Id private ObjectId id;
    @Embedded
    public ArrayList<Files> fileObjects = new ArrayList<Files>();
}

UPDATE .. 的 在Morphia Updating阅读wiki不要“真的”说明只有当数组包含Integer时才能这样做:

  @Embedded
   List<Integer> roomNumbers = new ArrayList<Integer>();

以下是我到目前为止的尝试:

mongo.createUpdateOperations(BatchData.class).add("fileObjects", Files, false);

此代码插入的Files已经在mongo中。 false没有检测到它并将其插入数组的末尾。我可以向Files添加唯一ID,以便检测到数组中是否存在Files插入,然后只更新它?

@Embedded 
public class Files
{ 
    public Files() {

    }

    public int position;
    public String fileName = "";
    public String fileHash = "";

    public Files(int pos, String fileName, String fileHash) {
        this.position = pos;
        this.fileName = fileName;
        this.fileHash = fileHash;
    }
} 

阅读morphia-mongodb-accessing等其他答案,但他已经有了 @Entity BlogEntry(参见链接)在mongo之外的POJO中。也许我必须这样做? 拉出来,换掉它并保存回去?

3 个答案:

答案 0 :(得分:6)

回答我自己的问题让任何人高兴。

我想我解决了它并不确定 当fileObjects有很多Files时,看起来它正在进行测试 右fileHash确实已更新。

UpdateOperations<BatchData>updateOperations=mongo.createUpdateOperations
             (BatchData.class)
            .disableValidation().set("fileObjects.$.fileHash",hash).enableVali..;

mongo.update(mongo.createQuery(BatchData.class)
            .filter("uuid",theBatch.uuid)
            .filter("fileObjects.fileName","theFileName"),updateOperations);

答案 1 :(得分:1)

在我的情况下,我可以使用removeAll方法:

UpdateOperations<MyType> ops = ds.createUpdateOperations(
            MyType.class).removeAll("myEmbeddedList", "thevalue");
ds.update(ds.find(MyType.class).get(), ops);

这假设MyType有字段List myEmbeddedList;并且有一个从该列表中删除的“thevalue”。

答案 2 :(得分:0)

我只是(是的,现在)以这种方式解决了这个问题: 我得到了带有查询的根类

List<Applications> fileList = mDatastore.createQuery(File.class).field("_id").equal(new ObjectId(ID)).asList();

然后,我在这个根类中实现了CRUD操作(我将发布我的根类):

@Entity
public class Applications extends BaseEntity{


    private String name;    
    private String owner;   
    private String version;

    @Embedded
    List<Resources> resources = new ArrayList<>();

    @Embedded
    List<Activities> activities = new ArrayList<>();

    @Embedded
    List<Components> components = new ArrayList<>();

    public Applications() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getVersion() {
        return version;
    }

    public Applications setVersion(String version) {
        this.version = version;
        return this;
    }

    public List<Resources> getResources() {
        return resources;
    }

    public Applications setResources(List<Resources> resources) {
        this.resources = resources;
        return this;
    }

    public Resources getResourcesByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return resource;
        }
        return null;
    }

    public boolean containsResourceByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return true;
        }
        return false;
    }



    public Applications addResource(Resources newResource) {
        if (!containsResourceByName(newResource.getName())) {
          this.resources.add(newResource);
        }
        return this;
      }

    public Applications removeResource(Resources newResource) {
        if (containsResourceByName(newResource.getName())) {
          this.resources.remove(getResourcesByName(newResource.getName()));
        }
        return this;
    }

    public Applications removeResourceByName(String name) {
        if (containsResourceByName(name)) {
          this.resources.remove(getResourcesByName(name));
        }
        return this;
    }

    public List<Activities> getActivities() {
        return activities;
    }

    public Applications setActivities(List<Activities> activities) {
        this.activities = activities;
        return this;
    }

    public Activities getActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return activity;
        }
        return null;
    }

    public boolean containsActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addActivity(Activities newActivity) {
        if (!containsActivityByName(newActivity.getName())) {
          this.activities.add(newActivity);
        }
        return this;
      }

    public Applications removeActivity(Activities newActivity) {
        if (containsActivityByName(newActivity.getName())) {
          this.activities.remove(getActivityByName(newActivity.getName()));
        }
        return this;
    }

    public Applications removeActivityByName(String name) {
        if (containsActivityByName(name)) {
          this.activities.remove(getActivityByName(name));
        }
        return this;
    }

    public List<Components> getComponents() {
        return components;
    }

    public Applications setComponents(List<Components> components) {
        this.components = components;
        return this;
    }

    public Components getComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return component;
        }
        return null;
    }

    public boolean containsComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addComponent(Components newComponent) {
        if (!containsComponentByName(newComponent.getName())) {
          this.components.add(newComponent);
        }
        return this;
      }

    public Applications removeComponent(Components newComponent) {
        if (containsComponentByName(newComponent.getName())) {
          this.components.remove(getComponentByName(newComponent.getName()));
        }
        return this;
    }

    public Applications removeComponentByName(String name) {
        if (containsComponentByName(name)) {
          this.components.remove(getComponentByName(name));
        }
        return this;
    }

    public Applications updateComponentByName(String name, String newName) {
        if (containsComponentByName(name)) {
          getComponentByName(name).setName(newName);
        }
        return this;
    }


    @Override
    public String toString() {

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_ID, id);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_NAME, name);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_OWNER, owner);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_VERSION, version);
            jsonObject.put(DatabaseModel.ActivitiesModel.DOCUMENT_NAME, activities);
            jsonObject.put(DatabaseModel.ResourcesModel.DOCUMENT_NAME, resources);
            jsonObject.put(DatabaseModel.ComponentsModel.DOCUMENT_NAME, components);
            return jsonObject.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "Something went wrong";
        }

    }

有一次,我修改了我的应用程序数据,我只需将它保存到主程序中的mongodb:

mDatastore.save(mApplications);

希望这可以帮助你,再见!