0

I want to convert a CSV line into a GeoJSON object. I am using CSVReader. Hence, nextLine[] has all the separated tokens. I want to create BasicDBObject which has various attributes stored. I am doing it following way.

new BasicDBObject("attribute1",nextLine[0]).append("attribute2",nextLine[1])

What I want to achieve is having a document like this in MongoDB { attribute1: name attribute2: address location:{ type : "Point" , coordinates : [lat, long] } attrribute3: phonenumber } How do I do this using BasicDBObject?enter code here

koustubhC
  • 157
  • 2
  • 15

2 Answers2

0

The easiest way to do that is using the BasicDBObjectBuilder, an utility class to build DBObjects. You can do something like that:

BasicDBObject toInsert = BasicDBObjectBuilder.start()
    .add("attribute1",nextLine[0])
    .add("attribute2",nextLine[1])
    .add(" attrribute3",nextLine[2])
    .push("location")
        .add("type", "Point")
        .add("coordinates", new double[] { nextLine[3], nextLine[4] })
    .pop()
    .get()
Miguel Cartagena
  • 2,576
  • 17
  • 20
0

I did it the other way

double latLong[] = new double[]{10.0, 30.0};
BasicDBObject doc = new BasicDBObject("attr1",nextLine[0])
         .append("attr2", nextLine[1]
    .append("location",new BasicDBObject("type","Point")
.append("coordinates",latLong)) 
    .append("attr3", nextLine[3])

This also works as desired

koustubhC
  • 157
  • 2
  • 15