Java: Morphia how easy is it to update a Collection with new field values
I'm new to this,
Let's say I have Collection "UserData
" and there are 1000 Documents in the Us开发者_JAVA百科erData.
Now I need to add one more field called "position
".
Can I do this, how?, or how does it work?
Looking on the wiki Morphia but cannot find anything
if the UserData
is look like,
@Entity
UserData{
@Id
private ObjectId id;
private String name;
private int age;
}
and you have added 1000 documents in this Collection.
Now, you want one more field called position
,
Just do this,
@Entity
UserData{
@Id
private ObjectId id;
private String name;
private int age;
private String position;//Add the field like this.
}
In the old 1000 documents, the value of position will be null
.
Its the power of mongo, you can expand your data structure when ever you want.
Essentially, yeah, you can. There are implications, but significantly less than using a traditional ORM.
With Morphia, if your persisted object has an instance variable that isn't set, its not included in the record in mongo. So adding another instance variable is pretty straightforward for mongo to retroactively map older records - they just get null
for that variable (in my experience).
Now if your application now absolutely depends on Position you'll have to update those 1000 records, but in general in my experience you can add the instance variable and it'll just work, easy as that.
Maybe this page can help you with your problem. http://code.google.com/p/morphia/wiki/Updating
精彩评论