Really new to Using Mongodb with Morphia and see many advanced answers how to do this.
I want to do it simple if it's possible and I have this @Embedded
Object called
fileObjects
that contains Files
Objects.
I Cannot update the fields inside the Files
.
I want to update only one field f.ex the String fileHash
.
@Entity
public class BatchData {
@Id private ObjectId id;
@Embedded
public ArrayList<Files> fileObjects = new ArrayList<Files>();
}
UPDATE..
Reading the wiki at Morphia Updating dont "really" say how to do this only when the array contains Integer
like this:
@Embedded
List<Integer> roomNumbers = new ArrayList<Integer>();
Here is what i try so far:
mongo.createUpdateOperations(BatchData.class).add("fileObjects", Files, false);
The Files
that this code insert is already in the mongo. The false
don't detect that and insert it at the end of the array. Can i add an unique-id to the Files
so it detect that the Files
am inserting exist in the array, and then just update it?
@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;
}
}
Reading other answers like morphia-mongodb-accessing but he already have the
@Entity BlogEntry(see link) in a POJO outside of mongo. Maybe i have to do the same?
Pull it out, change it and save it back?
I just (yeah, right now) solved this problem, in this way: I get the root class with a query
Then, I have implemented the CRUD operations inside this root class (I'll post my root class):
Once, I modified my Application data I just save it to mongodb in the main program with:
Hope this can help you, bye!
In my case I was able to use the removeAll method:
This assume that MyType has the field List myEmbeddedList; and that there is a vlue "thevalue" to be removed from the list.
Answering my own question for anyone's delight.
I think i solved it not sure.
It looks like it's working im testing when the
fileObjects
have manyFiles
.The right
fileHash
is updated indeed.