Suresh Kumar Dhayal
Suresh Kumar Dhayal

Reputation: 35

How to insert GeoJson object as prototype $geometry in mongoDB collection through java

Currently I am making mongoDB database using java and I am stuck at inserting Geo location in collection. My mentor told me to insert Geo Location using GeoJSON object as $geometry prototype. What I mean is that, I want to insert data as following prototype -

$geometry: {
   type: "Polygon",
   coordinates: [ <coordinates> ],
   crs: {
      type: "name",
      properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
   }
}

And I did this -

double lat_lng_values[] = {144.6682361, -37.8978304};
List<BasicDBObject> loc = new ArrayList();
BasicDBObject obj = new  BasicDBObject();
obj.put("location",lat_lng_values);
PInfo.insert(obj);    //(Here Pinfo is DBCollection Object)

So please help me that how can I use $geometry prototype. Thank you in advance. Sorry for my English language.

Upvotes: 1

Views: 1584

Answers (1)

adnanmuttaleb
adnanmuttaleb

Reputation: 3614

You prototype does not convey to GeoJson standard, you should follow the following schema:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
} 

so if you have any attribute such as 'crs' it should be included in the properties part of GeoJson schema. and about inserting the GeoJson document in mangoDB I'm not expert with that but you could use some thing like that:

    Mongo mongo = new Mongo('your paramaters');
    DB db = mongo.getDB("yourdb");

    DBCollection collection = db.getCollection("dummyColl");
    String json = "{
        'type': 'Feature',
        'geometry': {
          'type': 'Point',
          'coordinates': [125.6, 10.1]
        },
       'properties': {
          'name': 'Dinagat Islands'
       }
   }";

    DBObject dbObject = (DBObject)JSON.parse(json);

    collection.insert(dbObject);

Upvotes: 1

Related Questions