Insert json file into mongodb

2019-01-21 01:51发布

问题:

I am new in mongodb. After installing mongodb in windows I am trying to insert a simple json file using the following command:

C:\>mongodb\bin\mongoimport --db test --collection docs < example2.json

I am getting the following error:

connected to: 127.0.0.1
Fri Oct 18 09:05:43.749 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:43
Fri Oct 18 09:05:43.750
Fri Oct 18 09:05:43.750 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:42
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:44
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 check 0 0
Fri Oct 18 09:05:43.752 imported 0 objects
Fri Oct 18 09:05:43.752 ERROR: encountered 6 error(s)s

example2.file

{"FirstName": "Bruce", "LastName": "Wayne", 
"Email": "bwayne@Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox", 
"Email": "lfox@Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson", 
"Email": "dgrayson@Wayneenterprises.com"}

What do I need to do to import new json file into mongodb?

回答1:

Use

mongoimport --jsonArray --db test --collection docs --file example2.json

Its probably messing up because of the newline characters.



回答2:

Below command worked for me

mongoimport --db test --collection docs --file example2.json

when i removed the extra newline character before Email attribute in each of the documents.

example2.json

{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bwayne@Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox", "Email": "lfox@Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson", "Email": "dgrayson@Wayneenterprises.com"}


回答3:

This worked for me - ( from mongo shell )

var file = cat('./new.json');     # file name
use testdb                        # db name
var o = JSON.parse(file);         # convert string to JSON
db.forms.insert(o)                # collection name


回答4:

Use below command while importing JSON file

C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json


回答5:

mongoimport --jsonArray  -d DatabaseN -c collectionName /filePath/filename.json


回答6:

In MS Windows, the mongoimport command has to be run in a normal Windows command prompt, not from the mongodb command prompt.



回答7:

It happened to me couple of weeks back. The version of mongoimport was too old. Once i Updated to latest version it ran successfully and imported all documents.

Reference: http://docs.mongodb.org/master/tutorial/install-mongodb-on-ubuntu/?_ga=1.11365492.1588529687.1434379875



回答8:

the following two ways work well:

C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs < example2.json

if the collections are under a specific user, you can use -u -p --authenticationDatabase



标签: mongodb