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.
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.
Use the mongoimport utility to import data into a MongoDB database. MongoDB provides the mongoimport utility that can be used to import JSON, CSV, or TSV files into a MongoDB database. mongoimport is located in the bin directory (eg, /mongodb/bin or wherever you installed it).
I was able to fix the error using the following query:
mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray
Hopefully this is helpful to someone.
try this,
mongoimport --db dbName --collection collectionName <fileName.json
Example,
mongoimport --db foo --collection myCollections < /Users/file.json
connected to: *.*.*.*
Sat Mar 2 15:01:08 imported 11 objects
Issue is because of you date format.
I used same JSON with modified date as below and it worked
{jobID:"2597401",
account:"XXXXX",
user:"YYYYY",
pkgT:{"pgi/7.2-5":{libA:["libpgc.so"],flavor:["default"]}},
startEpoch:"1338497979",
runTime:"1022",
execType:"user:binary",
exec:"/share/home/01482/XXXXX/appker/ranger/NPB3.3.1/NPB3.3-MPI/bin/ft.D.64",
numNodes:"4",
sha1:"5a79879235aa31b6a46e73b43879428e2a175db5",
execEpoch:1336766742,
execModify:{"$date" : 1343779200000},
startTime:{"$date" : 1343779200000},
numCores:"64",
sizeT:{bss:"1881400168",text:"239574",data:"22504"}}
hope this helps
Using mongoimport you can able to achieve the same
mongoimport --db test --collection user --drop --file ~/downloads/user.json
where,
test - Database name
user - collection name
user.json - dataset file
--drop
is drop the collection if already exist.
console:
mongoimport -d dbName -c collectionName dataFile.js
I have used below command for export DB
mongodump --db database_name --collection collection_name
and below command worked for me to import DB
mongorestore --db database_name path_to_bson_file
Your syntax appears completely correct in:
mongoimport --db dbName --collection collectionName --file fileName.json
Make sure you are in the correct folder or provide the full path.
Run the import command in another terminal. (not inside mongo shell.)
mongoimport --db test --collection user --drop --file ~/downloads/user.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With