2015-05-11 139 views
1

我剛開始使用JavaMongoDB。我想從我的數據庫中檢索一些數據並將其存儲在列表中。我想獲得一個只有座標的列表。 (請參閱樣本JSON代碼)。檢索MongoDB數據並將其存儲在列表中

我相信我目前有一個包含集合中所有對象的列表。我只想從數據中獲得座標,並將其存儲到列表中,我真的不知道該怎麼做。 這是我的Java代碼(connectToMongoCollection方法使(顯然)連接到我的數據庫:

DBCollection collection = DBCollections.connectToMongoCollection("collection"); 
     BasicDBList basicDBList = new BasicDBList(); 
     DBCursor cursor = collection.find(new BasicDBObject("type", "feature")); 


    try { 
     while(cursor.hasNext()) { 
      basicDBList.add(cursor.next()); 
     } 
    } finally { 
     cursor.close(); 
    } 

    for(Object object: basicDBList){ 
     BasicDBObject basicDBObject = (BasicDBObject) object; 

    } 

這是一個示例MongoDB文檔的格式

"features": [ 
    {"type": "Feature", 
    "properties": { "OBJECTID": 1, "Join_Count": 1, "LABEL": 0 }, 
    "geometry": { "type": "MultiPoint", "coordinates": [ [ 4.3434010517041, 51.891054440280314 ] ] } } 

我希望有人能幫助我。在此先感謝。

+0

遵循此處討論 - http://stackoverflow.com/questions/24222545/how-to-access-object-nested-inside-an-array-in-mongodb-using-java-driver –

回答

1

此代碼會給你從存儲在BasicDBList數據座標。

public static void main(String[] args) { 
    try { 
     MongoClient mongoClient = new MongoClient("localhost", 27017); 
     DB db = mongoClient.getDB("StackOverflow"); 
     DBCollection dbcol = db.getCollection("features"); 

     DBCursor cursor = dbcol.find(); 

     try { 

      while (cursor.hasNext()) { 

       DBObject Features = cursor.next(); 

       BasicDBList features = (BasicDBList) Features.get("features"); 

       BasicDBObject[] featuresArr = features.toArray(new BasicDBObject[0]); 

       for (BasicDBObject dbobj : featuresArr) { 

        BasicDBObject geometry = (BasicDBObject) dbobj.get("geometry"); 

        BasicDBList coordinates = (BasicDBList) geometry.get("coordinates"); // BasicDBList contains coordinates 

        System.out.println(coordinates.get(0)); 

       } 

      } 
     } finally { 
      cursor.close(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

這一定是你的樣品MongoDB的文檔

{ "features": [ 
    { 
     "type": "Feature", 
     "properties": { 
      "OBJECTID": 1, 
      "Join_Count": 1, 
      "LABEL": 0 
     }, 
     "geometry": { 
      "type": "MultiPoint", 
      "coordinates": [ 
       [ 
        4.3434010517041, 
        51.891054440280314 
       ] 
      ] 
     } 
    } 
] 

}

和輸出是[ 4.3434010517041 , 51.891054440280314]

我的數據庫 「的StackOverflow」 插入這個文件和集合名稱爲 「特色」

0

這是另一種方法。不一定更好,但消化數據的人可能更容易。 我假設每個文檔都有一個特徵字段,它是一個包含座標數組的座標(不僅僅是一個座標)的數組。下面是收集富兩個這樣的文檔(刪除了其他字段並不重要):

db.foo.find().pretty(); 
{ 
"_id" : ObjectId("55735cccdbc638d309795958"), 
"features" : [ 
    { 
     "geometry" : { 
      "type" : "MultiPoint", 
      "coordinates" : [ [ 1.2, 3.4 ] ] 
     } 
    }, 
    { 
     "geometry" : { 
      "type" : "MultiPoint", 
      "coordinates" : [ [ 5.6, 7.8 ] , [ 9.1, 11.12 ] ] 
     } 
    } 
] 
} 
{ 
"_id" : ObjectId("55735cccdbc638d309795959"), 
"features" : [ 
    { 
     "geometry" : { 
      "type" : "MultiPoint", 
      "coordinates" : [ [ 83, 94 ] , [ 0.4, 0.5 ] ] 
     } 
    }, 
    { 
     "geometry" : { 
      "type" : "MultiPoint", 
      "coordinates" : [ [ 8.3434010517041, 9.891054440280314 ] ] 
     } 
    } 
] 
} 

下面是使用聚合框架來放鬆「陣內陣」給你6所有的好方法座標對你尋求。

db.foo.aggregate([ 
... {$unwind: "$features"} 
... , {$project: { coord: "$features.geometry.coordinates", "_id":0} } 
... , {$unwind: "$coord"} 
...     ]); 
{ "coord" : [ 1.2, 3.4 ] } 
{ "coord" : [ 5.6, 7.8 ] } 
{ "coord" : [ 9.1, 11.12 ] } 
{ "coord" : [ 83, 54 ] } 
{ "coord" : [ 0.4, 0.5 ] } 
{ "coord" : [ 8.3434010517041, 9.891054440280314 ] } 
相關問題