gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:57:44 24 4
gpt4 key购买 nike

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

现在我想从 [] 括号内的 listEditions 内的 insideDocument listInsertion 获取值。

那么如何获得它。

最佳答案

你可以使用这样的东西:

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 和其他检查。

关于java - 如何使用java从嵌入在mongodb数组中的内部文档中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com