Basic mongodump of local default mongod instance
mongodump --db mydb --gzip --out "mydb.dump.$(date +%F_%R)"
This command will dump a bson gzipped archive of your local mongod ‘mydb’ database to the ‘mydb.dump.{timestamp}’ directory
Basic mongorestore of local default mongod dump
mongorestore --db mydb mydb.dump.2016-08-27_12:44/mydb --drop --gzip
This command will first drop your current ‘mydb’ database and then restore your gzipped bson dump from the ‘mydb mydb.dump.2016-08-27_12:44/mydb’ archive dump file.
Mongoimport with JSON
Sample zipcode dataset in zipcodes.json stored in c:\Users\yc03ak1\Desktop\zips.json
{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
{ "_id" : "01002", "city" : "CUSHMAN", "loc" : [ -72.51564999999999, 42.377017 ], "pop" : 36963, "state" : "MA" }
{ "_id" : "01005", "city" : "BARRE", "loc" : [ -72.10835400000001, 42.409698 ], "pop" : 4546, "state" : "MA" }
{ "_id" : "01007", "city" : "BELCHERTOWN", "loc" : [ -72.41095300000001, 42.275103 ], "pop" : 10579, "state" : "MA" }
{ "_id" : "01008", "city" : "BLANDFORD", "loc" : [ -72.936114, 42.182949 ], "pop" : 1240, "state" : "MA" }
{ "_id" : "01010", "city" : "BRIMFIELD", "loc" : [ -72.188455, 42.116543 ], "pop" : 3706, "state" : "MA" }
{ "_id" : "01011", "city" : "CHESTER", "loc" : [ -72.988761, 42.279421 ], "pop" : 1688, "state" : "MA" }
to import this data-set to the database named “test” and collection named “zips”
C:\Users\yc03ak1>mongoimport --db test --collection "zips" --drop --type json --host
"localhost:47019" --file "c:\Users\yc03ak1\Desktop\zips.json"
- –db : name of the database where data is to be imported to
- –collection: name of the collection in the database where data is to be improted
- –drop : drops the collection first before importing
- –type : document type which needs to be imported. default JSON
- –host : mongodb host and port on which data is to be imported.
- –file : path where the json file is
output :
2016-08-10T20:10:50.159-0700 connected to: localhost:47019
2016-08-10T20:10:50.163-0700 dropping: test.zips
2016-08-10T20:10:53.155-0700 [################……..] test.zips 2.1 MB/3.0 MB (68.5%)
2016-08-10T20:10:56.150-0700 [########################] test.zips 3.0 MB/3.0 MB (100.0%)
2016-08-10T20:10:57.819-0700 [########################] test.zips 3.0 MB/3.0 MB (100.0%)
2016-08-10T20:10:57.821-0700 imported 29353 documents
Mongoimport with CSV
Sample test dataset CSV file stored at the location c:\Users\yc03ak1\Desktop\testing.csv
id city loc pop state
1 A [10.0, 20.0] 2222 PQE
2 B [10.1, 20.1] 22122 RW
3 C [10.2, 20.0] 255222 RWE
4 D [10.3, 20.3] 226622 SFDS
5 E [10.4, 20.0] 222122 FDS
to import this data-set to the database named “test” and collection named “sample”
C:\Users\yc03ak1>mongoimport --db test --collection "sample" --drop --type csv --headerline --host "localhost:47019" --file "c:\Users\yc03ak1\Desktop\testing.csv"
- –headerline : use the first line of the csv file as the fields for the json document
output :
2016-08-10T20:25:48.572-0700 connected to: localhost:47019
2016-08-10T20:25:48.576-0700 dropping: test.sample
2016-08-10T20:25:49.109-0700 imported 5 documents
OR
C:\Users\yc03ak1>mongoimport --db test --collection "sample" --drop --type csv --fields _id,city,loc,pop,state --host "localhost:47019" --file c:\Users\yc03ak1\Desktop\testing.csv"
- –fields : comma seperated list of fields which needs to be imported in the json document. Output:
2016-08-10T20:26:48.978-0700 connected to: localhost:47019
2016-08-10T20:26:48.982-0700 dropping: test.sample
2016-08-10T20:26:49.611-0700 imported 6 documents