96

I am new to 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.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"}

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

KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44
Jay
  • 1,181
  • 1
  • 9
  • 10

13 Answers13

129

Use

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

Its probably messing up because of the newline characters.

qed
  • 22,298
  • 21
  • 125
  • 196
Leon
  • 5,701
  • 3
  • 38
  • 38
  • This command should be run from the directory where 'mongoimport.exe' is present. The JSON file should also be present in that directory. – Samdish Arora Apr 30 '20 at 18:37
  • the command should be run inside the data/db folder, unless you will get an error. – Zeinab Aug 22 '20 at 07:13
  • You can run the command anywhere on the file system as long as "mongoimport" is in the path. The JSON file can be anywhere on disk as long as you include the path: mongoimport --jsonArray --db test --collection docs --file c:\junk\cc.json – Bruce Loth Mar 04 '21 at 00:23
64

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"}
Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
42

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
nanusdad
  • 446
  • 4
  • 5
  • 1
    That cool. To use mongo shell as an interpreter for the os and use "bash" commands as functions. I didn't even know that mongo can access the file tree like this. Thanks! – Rea Haas Sep 15 '20 at 10:46
  • After reading a file content with "cat", I am getting "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" at JSON.parse(file). If I replace "cat" with constant json string, parsing works! – uzrgm Nov 23 '21 at 10:18
  • Does this work in plain Node JS? I will try. – Timo Apr 19 '22 at 18:51
  • Yes it works in a JS file on the server, but why `insertOne` and not `insertmany`? The mongo shell seems like a mix of bash and JS. – Timo Apr 19 '22 at 18:58
  • The mongoimport tool imports content from an Extended JSON , CSV, or TSV export created by mongoexport, or potentially, another third-party export tool. Run mongoimport from the system command line, not the mongo shell. – krishnA tiwari Aug 24 '23 at 07:27
17

Use below command while importing JSON file

C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
Sumit Kamboj
  • 846
  • 2
  • 10
  • 17
5

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

dkb
  • 4,389
  • 4
  • 36
  • 54
user10383785
  • 51
  • 1
  • 1
5

This solution is applicable for Windows machine.

  1. MongoDB needs data directory to store data in. Default path is C:\data\db. In case you don't have the data directory, create one in your C: drive, unless different VolumeName is used e.g. H: (or any other relevant VolumeName) which is the root of your machine;

  2. Place the .json file you want to import within: C:\data\db\ .

  3. Before running the command copy-paste mongoimport.exe from C:\Program Files\MongoDB\Tools\100\bin (default path for mongoimport.exe) to the directory of the C:\Program Files\MongoDB\Server\[your_server_version]\bin

  4. Open the command prompt from within C:\data\db\ and type the following command by supporting the specific databasName, collectionName and fileName.json you wish to import :

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

Hereby,

  • batchSize can be any integer as per your wish
projektorius96
  • 114
  • 2
  • 10
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
4
mongoimport --jsonArray  -d DatabaseN -c collectionName /filePath/filename.json
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
Vijay Prajapati
  • 208
  • 1
  • 5
  • 19
2

Open command prompt separately and check:

C:\mongodb\bin\mongoimport --db db_name --collection collection_name< filename.json

Prakash Singh
  • 377
  • 2
  • 4
1

It works with JS and Node

Preconditions:

  • Node
  • Mongo - either local installed or via Atlas

server.js:

var MongoClient = require('mongodb').MongoClient;
var fs = require('fs')

export function insert(coll) {
  MongoClient.connect('uri', (err, db) => {
  var myobj = fs.readFileSync("shop.json").toString()
  myobj = JSON.parse(myobj)

  db.db(dbWeb).collection(coll).insertMany(myobj, (err, res) => {
    db.close();
  });
 });
}

shop.json:

[
{
    "doc": "jacke_bb",
    "link": "http://ebay.us/NDMJn9?cmpnId=5338273189",
},
{
    "doc": "schals",
    "link": "https://www.ebay-kleinanzeigen.de/s-anzeige/4-leichte-schals-fuer-den-sommer/2082511689-156-7597",
}

]

As one see, the json starts with [ and ends with ] and the insertMany is used. This leads to a correct nested insertion of the array into the collection.

Timo
  • 2,922
  • 3
  • 29
  • 28
0

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

0

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

aneeshep
  • 148
  • 5
0

In MongoDB To insert Json array data from file(from particular location from a system / pc) using mongo shell command. While executing below command, command should be in single 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": ""
}]
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
0

Run mongoimport from the system command line, not the mongo shell.