Timothy Rajan
Timothy Rajan

Reputation: 1957

MongoDB - how to insert a location into collection

I am looking a way to insert a location value to a collection in MongoDB. I am using MongoDB JAVA driver. Unfortunately I am not able to do so.

The collection is indexed properly. The issue here is the collection accepts the location in the form of an array which holds double value. But I am not sure whether there is a way to send an array directly since only the reference of the array is sent and not the actual content.

The code is below double latLong[] = {124.6682391, -17.8978304}; final BasicDBObject loc = new BasicDBObject(); loc.put("type","Point"); loc.put("coordinates", latLong);

jsonObject.put("location", loc);

After adding when I tried printing it I am getting the following output.

"location" : { "type" : "Point" , "coordinates" : "[D@53e21fa6"} 

This results in "Can't extract geo keys from object, malformed geometry?:" error.

I tried sending location as an arraylist. But this again stores the value as

"location" : { "type" : "Point" , "coordinates" : "[144.6682362, -37.8978302]"} 

But not as

"location" : { "type" : "Point" , "coordinates" : [144.6682362, -37.8978302]} 

This again results in "Can't extract geo keys from object, malformed geometry?:" error.

Also tried Arrays.toString((latLong))

This resulted in

" location" : { "type" : "Point" , "coordinates" : "[144.6682362, -37.8978302]"} 

Hence same error again.

The below URL says that this cannot be accomplished. https://groups.google.com/forum/#!topic/mongodb-user/TUjAxag6yT4

But still some part of my brain says there should be a way.

Any idea how to add a location object ( which is an array holding double value) to the JSON object and thereby to the collection by converting the JSON object to a DBObject?

I am not looking for a POJO library since I want to stick to my native code. If nothing can be done, I may jump to a POJO library.

Upvotes: 3

Views: 3306

Answers (2)

oggo
oggo

Reputation: 117

The question is pretty old, but newly i faced similar issue and found easy way of doing it using mongo-java-driver-2.13.3.jar downloaded from sonatype The one possibility is directly to put the coordinates in a BasicDBObject like here:

double latLong[]= {124.6682391, -17.8978304};
BasicDBObject loc1= new BasicDBObject("location", 
                     new BasicDBObject("type", "Point")
                    .append("coordinates", latLong));

System.out.println("DEBUG: loc1 is: " + loc1.toString());

The result is:

DEBUG: loc1 is: { "location" : { "type" : "Point" , "coordinates" : [ 124.6682391 , -17.8978304]}}

Another possibility which i found even better is to put BasicDBList like here:

BasicDBList coords= new BasicDBList();
coords.add(Double.valueOf(124.6682391));
coords.add(Double.valueOf(-17.8978304));
BasicDBObject loc2= new BasicDBObject("location", 
    new BasicDBObject("type", "Point")
   .append("coordinates", (latLong)));

System.out.println("DEBUG: loc2 is: " + loc2.toString());

The result is again:

DEBUG: loc2 is: { "location" : { "type" : "Point" , "coordinates" : [ 124.6682391 , -17.8978304]}}

Upvotes: 0

Jhanvi
Jhanvi

Reputation: 5139

You have to create jsonarray of co-ordinates and then put it in jsonObject. Try something like this:

        double latLong[] = {124.6682391, -17.8978304};
        JSONArray jsonArray = new JSONArray(latLong);
        JSONObject jobj = new JSONObject().put("type", "point");
        jobj.put("coordinates", jsonArray);

        // below  jsonObject_loc contains the jsonobject as you want..
        JSONObject jsonObject_loc = new JSONObject();
        jsonObject_loc.put("loc", jobj);
        System.out.println(jsonObject_loc);

       // but you have to store jobj in db as your query already has 'loc' object
        BasicDBObject loc = new BasicDBObject();
        loc.put("loc", jobj.toString());

The JSON Library used for the above code is : java-json

Upvotes: 3

Related Questions