Insert json file in mongodb

I am new to mongodb. After installing mongodb on windows, I try to insert a simple json file using the following command:

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

I get 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 a new json file into mongodb?

+66
mongodb
Oct 18 '13 at 3:45
source share
10 answers

Use

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

It is probably ruined due to newline characters.

+95
Dec 15 '13 at 3:05
source share

The below team worked for me

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

when I removed the extra newline before the 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"} 
+48
Oct 18 '13 at 4:01
source share

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 
+26
Sep 21 '16 at 15:08
source share

Use the command below when importing a JSON file

 C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json 
+15
Jul 25 '14 at 11:38
source share
 mongoimport --jsonArray -d DatabaseN -c collectionName /filePath/filename.json 
+2
Aug 02 '17 at 11:04 on
source share

The following two methods 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 belong to a specific user, you can use -u -p --authenticationDatabase

+1
Sep 19 '18 at 5:40
source share

On MS Windows, the mongoimport command should be run on a regular Windows command line, and not on the mongodb command line.

0
Jul 31. '14 at 20:37
source share

It happened to me a couple of weeks ago. The mongoimport version was too old. After I upgraded to the latest version, it successfully completed and imported all the documents.

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

0
Jun 17 '15 at 12:20
source share

This solution is applicable to a Windows machine.

  1. MongoDB needs a data directory to store data. The default path is C:\data\db . If you do not have a data directory, create it on the C: drive. (PS: data \ db means that inside the directory 'data' there is a directory named 'db')

  2. Put the json you want to import into this path: C:\data\db\ .

  3. Open a command prompt and enter the following command

    mongoimport --db databaseName --collections collectionName --file fileName.json --type json --batchSize 1

Here

  • database_name: the name of your database
  • collectionName: the name of your collection
  • fileName: the name of your JSON file, which is located in the path C: \ data \ db
  • batchSize can be any integer as you wish
0
May 04 '19 at 9:31
source share

In MongoDB Insert Json array data from a file (from a specific location on the system / PC) using the mongo shell command. When executing the command below, it should be on one line.

var file = cat ('I: /data/db/card_type_authorization.json'); var o = JSON.parse (file); db.CARD_TYPE_AUTHORIZATION.insert (o);

JSON file: card_type_authorization.json

 [{ "code": "visa", "position": 1, "description": "Visa", "isVertualCard": false, "comments": "" },{ "code": "mastercard", "position": 2, "description": "Mastercard", "isVertualCard": false, "comments": "" }] 
0
Sep 10 '19 at 18:20
source share



All Articles