检索 MongoDB 数据并将其存储在列表中

Retrieve MongoDB data and store it in a list

我刚开始使用 JavaMongoDB。我想从我的数据库中检索一些数据并将其存储在列表中。我想得到一个只有 坐标 的列表。 (参见示例 JSON 代码)。

我相信我目前有一个包含集合中所有对象的列表。我只想要数据中的坐标,并将其存储到列表中,我真的不知道该怎么做。 这是我的 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 ] ] } }

希望有人能帮助我,在此先感谢。

此代码将为您提供存储在 BasicDBList 中的数据的坐标。

 public static void main(String[] args) {
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("Whosebug");
        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]

我将此文档插入数据库 "Whosebug" 并且集合名称是 "features"

这是另一种方法。不一定更好,但对消化数据的人来说可能更容易。 我假设每个文档都有一个 features 字段,它是一个包含坐标的数组,可以是坐标数组(不仅仅是一个)。以下是集合 foo 中的两个此类文档(删除了其他不重要的字段):

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

这里有一个很好的方法,可以使用聚合框架展开所有这些 "arrays within arrays",从而为您提供所需的 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 ] }