如何使用java从mongodb中的父路径中获取所有子项

时间:2014-12-02 13:43:15

标签: java mongodb

我的json数据看起来像吼叫。

{
    "_id" : "Assets",
    "Act_Type" : true,
    "Path" : null
}


{
    "_id" : "Bank",
    "Act_Type" : true,
    "Path" : ",Assets,"
}


{
    "_id" : "Cash",
    "Act_Type" : true,
    "Path" : ",Assets,"
}


{
    "_id" : "NRI",
    "Act_Type" : true,
    "Path" : ",Assets,Bank,"
}


{
    "_id" : "Local",
    "Act_Type" : true,
    "Path" : ",Assets,Bank,"
}


{
    "_id" : "SBI",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,Local,"
}


{
    "_id" : "Canara",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,Local,"
}


{
    "_id" : "ICICI",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,NRI,"
}


{
    "_id" : "HDFC",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,NRI,"
}

当我尝试从父路径中检索数据而不是获取所有值时,我正在使用具有物化路径的模型树结构例如:我的路径是“,资产”,我只是

{
    "_id" : "Bank",
    "Act_Type" : true,
    "Path" : ",Assets,"
}

但我需要

{
    "_id" : "Bank",
    "Act_Type" : true,
    "Path" : ",Assets,"
}


{
    "_id" : "Cash",
    "Act_Type" : true,
    "Path" : ",Assets,"
}


{
    "_id" : "NRI",
    "Act_Type" : true,
    "Path" : ",Assets,Bank,"
}


{
    "_id" : "Local",
    "Act_Type" : true,
    "Path" : ",Assets,Bank,"
}


{
    "_id" : "SBI",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,Local,"
}


{
    "_id" : "Canara",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,Local,"
}


{
    "_id" : "ICICI",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,NRI,"
}


{
    "_id" : "HDFC",
    "Act_Type" : false,
    "Path" : ",Assets,Bank,NRI,"
}

PLZ帮助我如何使用java ....

进行查询

2 个答案:

答案 0 :(得分:0)

尝试

DB db = mongoClient.getDB( "mydb" );
DBCollection collection = db.getCollection("testCollection");
DBObject query = new BasicDBObject("Path", new BasicDBObject("$exists", true));
DBCursor cursor = collection.find(query);
try {
 while(cursor.hasNext()) {
   System.out.println(cursor.next());
 }
} finally {
 cursor.close();
}

答案 1 :(得分:0)

您似乎需要使用regex,因为Path的内容是String,而不是Array

试试这个:

DBObject query = new BasicDBObject("Path", new BasicDBObject("$regex", "*Asset*"));