3

モルフィアでMongodbを使用するのは本当に初めてで、これを行う方法について多くの高度な回答を参照してください。

可能であれば簡単にしたいと思います。オブジェクトを含むこの@Embeddedオブジェクトを呼び出します。
fileObjectsFiles

内のフィールドを更新できませんFiles

1つのフィールドf.exのみを更新したいString fileHash

@Entity
public class BatchData {

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

UPDATE .. Morphia Update でwikiを読んで、配列に次のIntegerようなものが含まれている場合にのみ、これを行う方法を「実際に」言ってはいけません。

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

これが私がこれまでに試したことです:

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

このFilesコード挿入はすでにmongoにあります。それfalseを検出せず、配列の最後に挿入してください。Files配列に挿入しているものが存在することを検出するようにunique-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のような他の回答を読んでいますが、彼はすでに
mongoの外部のPOJOに@Entity BlogEntry(リンクを参照)を持っています。多分私は同じことをしなければなりませんか?
引き出して変更し、保存し直しますか?

4

3 に答える 3

7

誰かの喜びのために私自身の質問に答えます。

よくわからないと思います。がたくさん
ある場合は、imテストで機能しているようです。 権利は確かに更新されます。fileObjectsFiles
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);
于 2012-02-09T23:33:06.603 に答える
1

私の場合、removeAllメソッドを使用できました。

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

これは、MyTypeのフィールドがListmyEmbeddedListであると想定しています。そして、リストから削除される「値」の値があること。

于 2013-08-31T13:41:46.487 に答える
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);

さようなら、これがあなたを助けることができることを願っています!

于 2015-10-20T20:32:31.907 に答える