2012-02-08 73 views
3

對於Morphia使用Mongodb並看到許多高級答案如何實現這一點真的很新穎。在Morphia中如何更新ArrayList中的一個嵌入對象

我希望它簡單,如果這是可以做到的,我有這個@Embedded對象調用
fileObjects包含Files對象。

我無法更新Files中的字段。

我想只更新一個字段f.ex String fileHash

@Entity 
public class BatchData { 

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

UPDATE .. 讀數Morphia Updating維基不「真」說,如何做到這一點,只有當數組包含Integer這樣的:

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

這是我嘗試到目前爲止:

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

該代碼插入的Files已經在mongo中。 false未檢測到並將其插入到數組的末尾。我可以添加一個唯一的ID到Files,以便它檢測到插入的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(見鏈接)蒙戈之外的POJO。也許我必須這樣做?
拉出來,改變它並保存回去?

回答

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); 
+0

它工作得很好。 Morphia應該在本地支持它。現在我們必須禁用使用這個「$」操作符的驗證。 – 2012-11-16 02:17:56

1

在我來說,我能夠使用RemoveAll方法:

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

這個假設的MyType具有字段列表myEmbeddedList;而且有一個「價值」從名單中刪除。

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

希望這能幫助你,再見!

相關問題