MongoDB Embedded object creation in Java without morphia.
I am very new to mongodb and tryn to use it for developement. I have a conceptual model of:
User={"uid":"", "开发者_运维问答services":"[
{
"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},
{
"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},
{
"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},
]",
"friends":"[
{
"friend_id":"",
"friendname":"name"
"friendshipyrs":"yrs",
},
{
"friend_id":"",
"friendname":"name"
"friendshipyrs":"yrs"
},
]",
}
I wish to know the steps to follow in java with the raw driver, not morphia to, to: 1. create this object, such that i have the ability to: 2. fetch and append new services and friends to the service lists.
I can currently add to the top level and I tried using the BasicDBObject, DBList, and even the ObjectBUilder but could not figure out how to append or push into the fields to create the arrays/lists with the java driver as can be seen from the presentations on the 10gen site.
Also, want to be able to drill down to say.. friend info with a single query, so will it be advisable maintain above structure or create friends as a class and put the class objects in the list? I know of the dot operator, but i do not know how to access class fields through the java driver.
Will be very greatful for any help... Thank you
First of all it's easier to ask one question per post ;)
Doing updates with the java driver is pretty straightforward, if a bit verbose :
Say your mongo update would look like :
db.users.friends({_id: <someuserid>}, {$push:{friends:{friend_id:...., friendname: ....}}})
All you have to do in Java is create a DBObject that encapsulates that update, so in this case :
DBObject query = new BasicDBObject("_id", <someuserid>);
DBObject newFriend = new BasicDBObject("friend_id", ...);
newFriend.put("friendname", ....);
DBObject update = new BasicDBObject("$push", new BasicDBObject("friends", newFriend));
DBCollection col = db.getCollection("users");
col.update(query, update);
Excuse any typoes, I haven't actually syntax checked this or anything but it should get you started. It's worth noting that it's important to be able to do these things in shell before trying it in Java. The API als has QueryBuilder which eases query object construction.
Full API documentation can be found here : http://api.mongodb.org/java/2.6.3/
Like Remon said, I think it's a good idea to go with an ORM as it usually presents a more production-ready approach than rolling your own. It sounds like you want to use object references to me. By using the @Reference annotation, I am able to "embed" objects within another object and then once I've loaded the owning object, I can get access to the owned object (I think this uses eager loading). An example:
@Entity public class PlaylistItem extends SomeModel{
@Required
@Reference
public Playlist playlist;
@Required
@Reference
public Track track;
}
Playlist and Track are two models annotated with @Entity, just like this PlaylistItem object.
Hope this helps.
More info on Morphia annotations here: http://code.google.com/p/morphia/wiki/AllAnnotations. There is even an Embeddd annotation but I have never had to use that.
精彩评论