2

I am trying to create this query from Java for MongoDB. Do you know how can I construct it in java?

db.node.find({
 connectedWithIds: { $in: [ 
     ObjectId('56bca32fe74a987ad8724da1')       
     ] }
})

I tried to use this:

ObjectId arr[] = {new ObjectId("5409ae2e2cdc31c5aa0ce0a5")};
BasicDBObject inQuery = new BasicDBObject("$in", arr);
BasicDBObject query = new BasicDBObject("connectedWithIds", inQuery);

but the results is below:

{ "connectedWithIds" : { "$in" : [ { "$oid" : "5409ae2e2cdc31c5aa0ce0a5"}]}}

and occurs this error:

error: {
    "$err" : "Can't canonicalize query: BadValue cannot nest $ under $in",
    "code" : 17287
}

1 Answer 1

1

I could overcome this error with the code below:

    ArrayList<ObjectId> vals = new ArrayList<ObjectId>();
    vals.add(objectId);       
    BasicDBObject inQuery = new BasicDBObject("$in", vals);
    BasicDBObject query = new BasicDBObject("connectedWithIds", inQuery);
    List<BasicDBObject> users = (List<BasicDBObject>) customQueryManager.executeQuery("node", query);
Sign up to request clarification or add additional context in comments.

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.