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": "[email protected]"}
{"FirstName": "Lucius", "LastName": "Fox", 
"Email": "[email protected]"}
{"FirstName": "Dick", "LastName": "Grayson", 
"Email": "[email protected]"}

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

Solution 1

Use

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

Its probably messing up because of the newline characters.

Solution 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": "[email protected]"}
{"FirstName": "Lucius", "LastName": "Fox", "Email": "[email protected]"}
{"FirstName": "Dick", "LastName": "Grayson", "Email": "[email protected]"}

Solution 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

Solution 4

Use below command while importing JSON file

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

Solution 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

Solution 6

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

Solution 7

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

Solution 8

Open command prompt separately and check:

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

Solution 9

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

Solution 10

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

Solution 11

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": ""
}]

Solution 12

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.