简体   繁体   中英

Retrieve MongoDB data and store it in a list

I just started using Java and MongoDB . I want to retrieve some data from my database and store it in a list. I want to get a list with only coordinates . (see sample JSON code).

I believe I currently have a list with all the objects in the collection. I only want the coordinates from the data, and store it into a list, I really don't know how to do it. This is my java code(the connectToMongoCollection method makes (obviously) connection to my database:

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;

    }

This is the format of a sample MongoDB document.

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

I hope someone can help me, Thanks in advance.

This code will give you coordinates from data stored in a 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();
    }

}

This must be your sample mongodb document

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

}

and output is [ 4.3434010517041 , 51.891054440280314]

I inserted this document in database "StackOverflow" and collection name is "features"

Here's another way to do it. Not necessarily better, but perhaps easier on the person digesting the data. I am assuming that each doc has a features field that is an array of things including coordinates which can be array of coordinates (not just one). Here are two such docs in collection foo (removed the other fields that aren't important):

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

Here's a nice way to use the aggregation framework to unwind all those "arrays within arrays" to give you the 6 coordinate pairs you seek.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM