简体   繁体   English

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

[英]How to retrieve data from inner document embedded in array in mongo db using java

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();
        }

    }

I have above program which gives me output as below 我有上面的程序,它给我输出如下

{ "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"}

Now I want to fetch values from innerDocument listInsertion which is inside listEditions in [] brackets. 现在我想从innerDocument listInsertion中获取值,这些值位于[]括号中的listEditions内。

So how to get it. 那么如何得到它。

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

You can use something like this: 你可以使用这样的东西:

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"));
    }
}

Add instanceof and other checks if you need. 如果需要,添加instanceof和其他检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用java在Mongo DB中插入数据,并使用javascript从Mongo DB中检索数据 - Inserting data in Mongo DB using java and retrieve the data from Mongo DB using javascript 如何使用Mongo Java驱动程序从集合中检索随机文档 - How to retrieve a random document from a collection using the Mongo Java driver 如何使用spring mongo data api更新嵌入式mongo文档 - How to update embedded mongo document using spring mongo data api 如何使用 Spring Data Mongo DB 仅检索文档的特定字段? - How to retrieve only specific field of a document with Spring Data Mongo DB? 使用Java将子文档的内部子文档中的特定字段值搜索到Mongo db - search the particular field value in inner sub document of sub document to Mongo db using Java 如何使用JAVA一次更新mongo db的单个文档中的多个字段(数组字段和普通字段)? - How to update multiple fields(array field and normal field) in a single document of mongo db at a time using JAVA? 使用java Driver从mongo db获取嵌套文档 - Getting nested Document from mongo db using java Driver 在Java中,如何简单地从mongo文档中检索键? - In Java how can I simply retrieve the Keys from a mongo document? 使用 Spring Data Embedded Mongo 在 Mongo db 中导入 JSON 文件 - Import JSON file in Mongo db using Spring Data Embedded Mongo 使用Java从MySQL DB检索数据 - Retrieve data from MySQL DB using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM