1

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

2 Answers 2

5

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/

Sign up to request clarification or add additional context in comments.

4 Comments

As a small P.S., it's almost always a good idea to use an ORM library of some sort rather than using the Java driver directly because it makes code hard to read and overly verbose. Is there any reason you're actively avoiding Morphia and the like?
Thax very much Remon, Just noticed the response... will try it out and let you know. Im really grateful.
Im guessing i should create services and friends as a class object and $push instances then and retrieve fields with the dot operator...... I am avoiding morphia (for now) because i want to get a hang of what is under the hood first. Also i read that managing embedded objects is a little challenging through morphia. Guess that scared me because i want as much flexibility with the data as possible. And oh I will do well to ask 1 question per post next time :-)
Well, we wrote our own ORM that fit our needs a bit better but the point stands. Bare bones Java driver code is super verbose and will introduce bugs quite easily. Note that if you're more comfortable with the shell syntax you could go for DBObject query = JSON.parse("{\"friend._id\": ObjectId("982173981273891739812")}"); instead. Let's you write shell-like queries at the expense of validation and some performance.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.