1

I am trying to convert the mongodb shell code below to java. However, i run into some problems doing so. Can anyone help me with this issue?

MongoDB

var friend_ids = db.users.findOne(ObjectId("...")).friend_ids
db.users.find({_id:{$in:friend_ids}})

Java

ObjectId id = new ObjectId("...");                  
BasicDBObject fields = new BasicDBObject("friend_ids", 1).append("_id", false);
DBObject f_ids = coll.findOne(id, fields);
BasicDBObject query = new BasicDBObject("_id",(new BasicDBObject("$in", f_ids)));
DBCursor cursor = coll.find(query);

The java query is as follows.

query={ "_id" : { "$in" : { "friend_ids" : [ { "$oid" : "..."} , { "$oid" : "..."} , { "$oid" : "..."}]}}}, 

thanks

2
  • And what exactly are the problems you are running into? Commented Jan 31, 2014 at 9:54
  • I do not get the wanted friendlist within java;). Commented Jan 31, 2014 at 9:59

1 Answer 1

1

You need extract friend_ids from f_ids.

BasicDBObject query = new BasicDBObject("_id",(new BasicDBObject("$in", f_ids.get("friend_ids"))));
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks,That is really quick!. i will accept as correct as soon as possible (5min)

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.