在mongo shell中,我使用此查询检索元素的祖先(我使用祖先数组构建了一个树结构):
db.collection.findOne({_id: "some_unique_id"}).ancestors
Java中的等效代码是什么?
我的代码无法获得正确的结果:
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());
错误在哪里?
提前致谢
答案 0 :(得分:1)
与您的mongo shell查询等效的Java是:
BasicDBObject query = new BasicDBObject("_id", "some_unique_id");
DBObject o = locations.findOne(query);
System.out.println(o.get("ancestors"));