sickk
sickk

Reputation: 55

Find ancestors in mongodb with Java

In the mongo shell I retrieve ancestors of an element (I built a tree structure with an array of ancestors) using this query:

db.collection.findOne({_id: "some_unique_id"}).ancestors

What is the equivalent code in Java?

My code that doesn't get the right result is:

BasicDBObject root = new BasicDBObject();
root.put("_id", idObj);
root.put("type", typeObj);

BasicDBObject query = new BasicDBObject("ancestors", root);

DBObject o = locations.findOne(query);
System.out.println(idObj + " - findone => " + o.toString());

Where is the error?

Thanks in advance

Upvotes: 0

Views: 162

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311855

The Java equivalent to your mongo shell query is:

BasicDBObject query = new BasicDBObject("_id", "some_unique_id");
DBObject o = locations.findOne(query);
System.out.println(o.get("ancestors"));

Upvotes: 1

Related Questions