0
public static void main (String args[]) 
    {
        GetMorphiaDB morphia;
        //String id  = request.getParameter("id");
        try {
            morphia = GetMorphiaDB.getInstance();
            Map<String,Object> output = new LinkedHashMap<String, Object>();
            DB db = morphia.getDb();
            DBCollection collection = db.getCollection("Transactions");
            BasicDBObject allQuery = new BasicDBObject();
              BasicDBObject fields = new BasicDBObject();
              fields.put("referenceID", 1);

              /*DBCursor cursor2 = collection.find(allQuery, fields);
              while (cursor2.hasNext()) {
                System.out.println(cursor2.next());
              }*/

              //System.out.println("\n2. Find where number = 5");
              BasicDBObject whereQuery = new BasicDBObject();
              whereQuery.put("referenceID", "E13F31BC80CE");
              fields.put("referenceID", 1);
              //fields.put("listEditions", 1);
              fields.put("listEditions.listInsertion", 1);
              fields.append("_id",false);
              DBCursor curs = collection.find(whereQuery,fields);
              while (curs.hasNext()) {
                System.out.println(curs.next());
              }
} 
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        } 
        catch (MongoException e) {
            e.printStackTrace();
        }

    }

我有上面的程序,它给我的输出如下

{ "listEditions" : [ { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "14/06/2013" , "toDate" : "14/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "14/06/2013" , "toDate" : "14/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "11/06/2013" , "toDate" : "11/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]} , { "listInsertion" : [ { "page" : 2 , "fromDate" : "15/06/2013" , "toDate" : "15/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0} , { "page" : 2 , "fromDate" : "15/06/2013" , "toDate" : "15/06/2013" , "size" : 240 , "color" : 0 , "colorType" : "All Colour" , "pagePosition" : 2 , "pagePositionType" : "Regular Page" , "sizeDimention" : "12x20" , "width" : 12.0 , "height" : 20.0}]}] , "referenceID" : "E13F31BC80CE"}

现在我想从innerDocument listInsertion 中获取值,它在[]括号中的listEditions 内。

那么如何获得。

4

2 回答 2

1

那是 JSON,您可以使用标准 JSON 库对其进行解析,或者使用GSON将其映射到 Java 对象。

于 2013-06-13T13:05:46.873 回答
1

你可以使用这样的东西:

DBObject next = cursor.next();
BasicDBList listEditions = (BasicDBList)next.get("listEditions");
for(Object element: listEditions) {
    BasicDBList listInsertions = (BasicDBList)((BasicDBObject)element).get("listInsertion");
    for(Object lie: listInsertions) {
        System.out.println(lie);
        //System.out.println(((BasicDBObject)lie).get("fromDate"));
    }
}

instanceof如果需要,添加和其他检查。

于 2013-06-13T13:44:41.367 回答