Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing json from file into mongodb using mongoimport

Tags:

mongodb

I have my json_file.json like this:

[ {     "project": "project_1",     "coord1": 2,     "coord2": 10,     "status": "yes",     "priority": 7 }, {     "project": "project_2",     "coord1": 2,     "coord2": 10,     "status": "yes",     "priority": 7 }, {     "project": "project_3",     "coord1": 2,     "coord2": 10,     "status": "yes",     "priority": 7 } ] 

When I run the following command to import this into mongodb:

mongoimport --db my_db --collection my_collection --file json_file.json  

I get the following error:

Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot? 

If I add the --jsonArray flag to the command I import like this:

imported 3 documents 

instead of one document with the json format as shown in the original file.

How can I import json into mongodb with the original format in the file shown above?

like image 529
Cybernetic Avatar asked May 21 '15 17:05

Cybernetic


People also ask

How do I import a JSON file into MongoDB?

To import JSON file you need to follow the following steps: Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don't close this cmd to stay connected to the server. Step 2: Open another command prompt and run the mongo shell. Using the mongo command.

Can we import JSON in MongoDB?

The process to import JSON into MongoDB depends on the operating system and the programming language you are using. However, the key to importing is to access the MongoDB database and parsing the file that you want to import. You can then go through each document sequentially and insert into MongoDB.

Which is used for getting data from JSON file to MongoDB?

The Python language will be used to handle MongoDB and manipulate JSON data. The PyMongo library will be used to access MongoDB Database in Python because it contains the essential drivers required to connect Python to MongoDB.


1 Answers

The mongoimport tool has an option:

--jsonArray treat input source as a JSON array

Or it is possible to import from file containing same data format as the result of db.collection.find() command. Here is example from university.mongodb.com courseware some content from grades.json:

{ "_id" : { "$oid" : "50906d7fa3c412bb040eb577" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb578" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb579" }, "student_id" : 0,       "type" : "homework", "score" : 14.8504576811645 } 

As you can see, no array used and no comma delimiters between documents either.

I discover, recently, that this complies with the JSON Lines text format.

Like one used in apache.spark.sql.DataFrameReader.json() method.

like image 123
Alex Glukhovtsev Avatar answered Oct 06 '22 01:10

Alex Glukhovtsev