Eri*_*rik 3 java mongodb morphia
使用Mongodb和Morphia并看到很多高级答案如何做到这一点真的很新.
我希望它简单,如果这是可以做到的,我有这个@Embedded对象调用
fileObjects包含Files的对象.
我无法更新内部的字段Files.
我只想更新一个字段f.ex的String fileHash.
@Entity
public class BatchData {
@Id private ObjectId id;
@Embedded
public ArrayList<Files> fileObjects = new ArrayList<Files>();
}
Run Code Online (Sandbox Code Playgroud)
更新..
在Morphia阅读维基更新不要"真的"说明只有当数组包含Integer这样的时候才能这样做:
@Embedded
List<Integer> roomNumbers = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
以下是我到目前为止的尝试:
mongo.createUpdateOperations(BatchData.class).add("fileObjects", Files, false);
Run Code Online (Sandbox Code Playgroud)
在Files此代码插入已经在蒙戈.在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;
}
}
Run Code Online (Sandbox Code Playgroud)
阅读其他答案,如morphia-mongodb-access,但他已经
在mongo之外的POJO中拥有@Entity BlogEntry(请参阅链接).也许我必须这样做?
拉出来,换掉它并保存回去?
为任何人的喜悦回答我自己的问题.
我想我解决了它不确定.
当它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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6257 次 |
| 最近记录: |