如何使用java从mongodb中嵌入数组的内部文档中检索数据

时间:2013-06-13 13:02:11

标签: java mongodb

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内。

所以如何得到它。

2 个答案:

答案 0 :(得分:1)

这是JSON,您可以使用standard JSON library对其进行解析,或使用GSON将其映射到Java对象。

答案 1 :(得分: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和其他检查。