我有一個這樣的樹蒙戈:MongoDB的樹找到後裔的Java
> db.data.find()
{ "_id" : "07c", "path" : null }
{ "_id" : "a", "path" : "07c,a" }
{ "_id" : "b", "data" : "{\"c\":\"olaC\",\"d\":\"olaD\"}", "path" : "07c,a,b" }
{ "_id" : "c", "c" : "{\"c\":\"olaC\",\"d\":\"olaD\"}", "path" : "07c,a,b,c" }
{ "_id" : "d", "d" : "{\"d\":\"olaD\"}", "data" : "{\"c\":\"olaC\",\"d\":\"olaD\"}", "path" : "07c,a,b,c,d" }
我想檢索B的所有後代。
如果我這樣做是MongoDB的控制檯,我得到的後代精:
> db.data.find({ path: /,b,/ })
{ "_id" : "c", "c" : "{\"c\":\"olaC\",\"d\":\"olaD\"}", "path" : "07c,a,b,c" }
{ "_id" : "d", "d" : "{\"d\":\"olaD\"}", "data" : "{\"c\":\"olaC\",\"d\":\"olaD\"}", "path" : "07c,a,b,c,d" }
,但如果我這樣做在Java中:
BasicDBObject query = new BasicDBObject();
query.put("path", "/,"+array[i]+",/");
DBCursor cursor = coll.find(query);
while(cursor.hasNext()){
System.out.println(cursor.next().toString());
}
從調試器查詢中包含這樣的:{ 「path」:「/,b,/」}
我根本沒有後代...爲什麼這不起作用?
http://docs.mongodb.org/manual/tutorial/model-tree-structures-with-materialized-paths/
試試這個:query.put( 「路徑」,Pattern.compile( 「/」 +陣列[I] + 「/」)); –