import a mongodb tuple

Asked

Viewed 70 times

0

Hello, I’m importing a collection of documents from mongodb, I’m importing from a csv file, I’m having trouble importing key pairs:value, mongodb is recognizing everything as string, example: I want the document to be saved like this on import.

"coordinates" : {
        "latitude" : -9.646375,
        "longitude" : -35.728146
    },

But you’re being saved like this:

"coordinates" : "{\"latitude\" :-9.739.990.639.836.790, \"longitude\" : -3.664.870.887.994.760}"
}

The csv file has the coordinates field like this

coordinates
{"latitude" :-9.262.132.106.882.630, "longitude" : -3.793.454.647.064.200}

I would like help so that I can import the document and the database recognizes this dictionary as it really is, not as a string.

I appreciate all your help!

  • I don’t get it. Do you have the file, read it somewhere? With some language? How are you importing in Mongo?

  • It is in the shell of Mongo, there is a file that I import your data into the Mongo database. One of these data is composed(coordinates{lat,long}) and I don’t know how to import this data in accordance with the database.

1 answer

1


When you have a csv, the line break represents the value for the previous line key, so:

coordinates
{"latitude" :-9.262.132.106.882.630, "longitude" : -3.793.454.647.064.200}

It means that your "title" would be: coordinates, but its value is all this:

{"latitude" :-9.262.132.106.882.630, "longitude" : -3.793.454.647.064.200}

If you want to import a file directly into the shell, it could import its own . json, for example, which would be right.

We assume:

Contents of the file: file.json

{
  "Coordenadas": {
    "Latitude": 1231231,
    "Longitude": 212312312
  }
}

And the import in the shell:

mongoimport -d suaBase -c suaCollection --type json --file D:\file.json

And the final content at the base:

{
    "_id" : ObjectId("59524d366f8cb1943dc17220"),
    "Coordenadas" : {
        "Latitude" : 1231231,
        "Longitude" : 212312312
    }
}
  • Ah, thank you, I managed to solve this problem more brutally, because of the demand that was on top to be delivered, but with your knowledge I can improve the task, Thanks!

  • Thank you Aline!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.