[MongoDB]: restore vs import

Utility mongorestore allows you to load data into the database. It requires a file (dump) that contains a BSON . Such a file can be created using different tools mongodump .
The second utility mongoimport allows you to import data from files in JSON, CSV or TSV. Better if the data file is created with the help mongoexport , though potentially possible to use other generators. 
Dump created with mongoexport not preserve typing, so you need to use it with the use of head and already working on databases better think twice.
mongoimport provides more options: 
1. -type which has three possible values: json, csv, tsv. 
2. You can specify the -file parameter and send back the file name, but you can do without it.
They have more in common than differences. 
1. -host And -port parameters allow you to connect to another server or another port. 
2. Use the -db or -d can specify the base to which to write data. 
3. -collection or -c specifies a collection where the data will be installed. 
4. -drop destroys collection before will activate the data stack. 
5. Name dump file to be transmitted last.
Here, perhaps, the most important parameters. The rest can be found in the official documentation: mongorestore and mongoimport .
Examples of use:
mongoimport -d gallery -c albums albums.json
mongorestore -d gallery -c albums albums.bson


mongorestore
Synopsis
The mongorestore program writes data from a binary database dump created by mongodump to a MongoDB instance. mongorestore can create a new database or add data to an existing database.
mongorestore can write data to either mongod or mongos instances, in addition to writing directly to MongoDB data files without an active mongod.
Behavior
If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.
Remember the following properties of mongorestore behavior:
mongorestore recreates indexes recorded by mongodump.
all operations are inserts, not updates.
mongorestore does not wait for a response from a mongod to ensure that the MongoDB process has received or recorded the operation.
The mongod will record any errors to its log that occur during a restore operation, but mongorestore will not receive errors.
[Lab root @ myhostname /tmp/jsons]# ll -lhtr
total 44M
-rw-r--r-- 1 oracle dba 1.6K Oct 28 02:15 appConfig.bson
-rw-r--r-- 1 oracle dba 101 Oct 28 02:15 appConfig.metadata.json
-rw-r--r-- 1 oracle dba 46K Oct 28 02:16 appConfigN.bson
-rw-r--r-- 1 oracle dba 274 Oct 28 02:16 appConfigN.metadata.json
-rw-r--r-- 1 oracle dba 1.2K Oct 28 02:16 appConfigV.bson
-rw-r--r-- 1 oracle dba 102 Oct 28 02:16 appConfigV.metadata.json
-rw-r--r-- 1 oracle dba 828 Oct 28 02:16 cart.bson
-rw-r--r-- 1 oracle dba 131 Oct 28 02:16 cart.metadata.json
-rw-r--r-- 1 oracle dba 4.8K Oct 28 02:16 errors.bson
-rw-r--r-- 1 oracle dba 98 Oct 28 02:16 errors.metadata.json
-rw-r--r-- 1 oracle dba 43M Oct 28 02:16 events.bson
-rw-r--r-- 1 oracle dba 98 Oct 28 02:16 events.metadata.json
-rw-r--r-- 1 oracle dba 421 Oct 28 02:16 groups.bson
-rw-r--r-- 1 oracle dba 98 Oct 28 02:16 groups.metadata.json
-rw-r--r-- 1 oracle dba 0 Oct 28 02:16 lmrm__agg_pipeline_ttl.bson
-rw-r--r-- 1 oracle dba 279 Oct 28 02:16 lmrm__agg_pipeline_ttl.metadata.json
-rw-r--r-- 1 oracle dba 1.1K Oct 28 02:16 lmrm__audit_trail.bson
-rw-r--r-- 1 oracle dba 109 Oct 28 02:16 lmrm__audit_trail.metadata.json
-rw-r--r-- 1 oracle dba 218K Oct 28 02:16 lmrm__metadata2.bson
-rw-r--r-- 1 oracle dba 230 Oct 28 02:16 lmrm__metadata2.metadata.json
-rw-r--r-- 1 oracle dba 113 Oct 28 02:16 lmrm__preferences.bson
-rw-r--r-- 1 oracle dba 109 Oct 28 02:16 lmrm__preferences.metadata.json
-rw-r--r-- 1 oracle dba 2.6K Oct 28 02:16 modelingTechnique.bson
-rw-r--r-- 1 oracle dba 109 Oct 28 02:16 modelingTechnique.metadata.json
-rw-r--r-- 1 oracle dba 343 Oct 28 02:16 mongoSequence.bson
-rw-r--r-- 1 oracle dba 149 Oct 28 02:16 mongoSequence.metadata.json
-rw-r--r-- 1 oracle dba 0 Oct 28 02:16 orders_auto.bson
-rw-r--r-- 1 oracle dba 145 Oct 28 02:16 orders_auto.metadata.json
-rw-r--r-- 1 oracle dba 893K Oct 28 02:16 orders.bson
-rw-r--r-- 1 oracle dba 135 Oct 28 02:16 orders.metadata.json
-rw-r--r-- 1 oracle dba 2.8K Oct 28 02:16 ordersNew.bson
-rw-r--r-- 1 oracle dba 141 Oct 28 02:16 ordersNew.metadata.json
-rw-r--r-- 1 oracle dba 4.1K Oct 28 02:16 reports.bson
-rw-r--r-- 1 oracle dba 207 Oct 28 02:16 reports.metadata.json
-rw-r--r-- 1 oracle dba 1.5K Oct 28 02:16 submittersLobs.bson
-rw-r--r-- 1 oracle dba 151 Oct 28 02:16 submittersLobs.metadata.json
-rw-r--r-- 1 oracle dba 90 Oct 28 02:16 system.users.bson
-rw-r--r-- 1 oracle dba 104 Oct 28 02:16 system.users.metadata.json
-rw-r--r-- 1 oracle dba 11K Oct 28 02:16 userAccess.bson
-rw-r--r-- 1 oracle dba 102 Oct 28 02:16 userAccess.metadata.json
-rw-r--r-- 1 oracle dba 2.0K Oct 28 02:16 userAccessTest.bson
-rw-r--r-- 1 oracle dba 106 Oct 28 02:16 userAccessTest.metadata.json
-rw-r--r-- 1 oracle dba 7.0K Oct 28 02:16 userRoles.bson
-rw-r--r-- 1 oracle dba 101 Oct 28 02:16 userRoles.metadata.json
[Lab root @ myhostname /tmp/jsons]cd /opt/mongodb/bin
[Lab root @ myhostname /opt/mongodb/bin]# ./mongo --port 27020
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> 
rs2:PRIMARY> 
rs2:PRIMARY> show dbs
local 1.078125GB
rs2:PRIMARY> 
rs2:PRIMARY> exit
[Lab root @ myhostname /opt/mongodb/bin]# ./mongo --port 27020
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> 
rs2:PRIMARY> 
rs2:PRIMARY> use admin
switched to db admin
rs2:PRIMARY> 
rs2:PRIMARY> db.addUser({"user":"adm", "pwd":"pwd","roles":["dbAdminAnyDatabase","userAdminAnyDatabase","clusterAdmin","readWriteAnyDatabase"]})
{
 "user" : "adm",
 "pwd" : "8f1cf56da9da9e575e0c85ed1c5a4fba",
 "roles" : [
 "dbAdminAnyDatabase",
 "userAdminAnyDatabase",
 "clusterAdmin",
 "readWriteAnyDatabase"
 ],
 "_id" : ObjectId("544f37560e7962363b32fe51")
}
rs2:PRIMARY> 
rs2:PRIMARY> 
bye
[Lab root @ myhostname /opt/mongodb/bin]
[Lab root @ myhostname /opt/mongodb/bin]# ./mongo --port 27020 -u adm -p pwd --authenticationDatabase admin
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> 
rs2:PRIMARY> 
rs2:PRIMARY> 
bye
[Lab root @ myhostname /opt/mongodb/bin]# ./mongorestore --host myhostname --port 27020 -u adm -p pwd --db restore_db --authenticationDatabase admin /tmp/jsons
connected to: myhostname:27020
Tue Oct 28 02:33:29.078 /tmp/jsons/lmrm__agg_pipeline_ttl.bson
Tue Oct 28 02:33:29.078 going into namespace [restore_db.lmrm__agg_pipeline_ttl]
Tue Oct 28 02:33:29.165 Created collection restore_db.lmrm__agg_pipeline_ttl with options: { "create" : "lmrm__agg_pipeline_ttl", "flags" : 1 }
Tue Oct 28 02:33:29.165 file /tmp/jsons/lmrm__agg_pipeline_ttl.bson empty, skipping
Tue Oct 28 02:33:29.165 Creating index: { key: { _id: 1 }, ns: "restore_db.lmrm__agg_pipeline_ttl", name: "_id_" }
Tue Oct 28 02:33:29.165 Creating index: { key: { lastSeen: 1 }, ns: "restore_db.lmrm__agg_pipeline_ttl", name: "lastSeen_1", expireAfterSeconds: 3600 }
Tue Oct 28 02:33:29.174 /tmp/jsons/userRoles.bson
Tue Oct 28 02:33:29.174 going into namespace [restore_db.userRoles]
5 objects found
Tue Oct 28 02:33:29.176 Creating index: { key: { _id: 1 }, ns: "restore_db.userRoles", name: "_id_" }
Tue Oct 28 02:33:29.181 /tmp/jsons/userAccessTest.bson
Tue Oct 28 02:33:29.181 going into namespace [restore_db.userAccessTest]
7 objects found
Tue Oct 28 02:33:29.183 Creating index: { key: { _id: 1 }, ns: "restore_db.userAccessTest", name: "_id_" }
Tue Oct 28 02:33:29.187 /tmp/jsons/events.bson
Tue Oct 28 02:33:29.187 going into namespace [restore_db.events]
Tue Oct 28 02:33:32.042 Progress: 25494835/44518986 57% (bytes)
8784 objects found
Tue Oct 28 02:33:33.310 Creating index: { key: { _id: 1 }, ns: "restore_db.events", name: "_id_" }
Tue Oct 28 02:33:33.492 /tmp/jsons/lmrm__audit_trail.bson
Tue Oct 28 02:33:33.492 going into namespace [restore_db.lmrm__audit_trail]
3 objects found
Tue Oct 28 02:33:33.494 Creating index: { key: { _id: 1 }, ns: "restore_db.lmrm__audit_trail", name: "_id_" }
Tue Oct 28 02:33:33.500 /tmp/jsons/lmrm__metadata2.bson
Tue Oct 28 02:33:33.500 going into namespace [restore_db.lmrm__metadata2]
282 objects found
Tue Oct 28 02:33:33.510 Creating index: { key: { _id: 1 }, ns: "restore_db.lmrm__metadata2", name: "_id_" }
Tue Oct 28 02:33:33.598 Creating index: { key: { name: 1, field_name: 1 }, ns: "restore_db.lmrm__metadata2", name: "name_1_field_name_1" }
Tue Oct 28 02:33:33.674 /tmp/jsons/cart.bson
Tue Oct 28 02:33:33.674 going into namespace [restore_db.cart]
Tue Oct 28 02:33:33.681 Created collection restore_db.cart with options: { "create" : "cart" }
6 objects found
Tue Oct 28 02:33:33.681 Creating index: { key: { _id: 1 }, ns: "restore_db.cart", name: "_id_" }
Tue Oct 28 02:33:33.682 /tmp/jsons/appConfigV.bson
Tue Oct 28 02:33:33.682 going into namespace [restore_db.appConfigV]
1 objects found
Tue Oct 28 02:33:33.683 Creating index: { key: { _id: 1 }, ns: "restore_db.appConfigV", name: "_id_" }
Tue Oct 28 02:33:33.688 /tmp/jsons/orders_auto.bson
Tue Oct 28 02:33:33.688 going into namespace [restore_db.orders_auto]
Tue Oct 28 02:33:33.694 Created collection restore_db.orders_auto with options: { "create" : "orders_auto" }
Tue Oct 28 02:33:33.694 file /tmp/jsons/orders_auto.bson empty, skipping
Tue Oct 28 02:33:33.694 Creating index: { key: { _id: 1 }, ns: "restore_db.orders_auto", name: "_id_" }
Tue Oct 28 02:33:33.695 /tmp/jsons/mongoSequence.bson
Tue Oct 28 02:33:33.695 going into namespace [restore_db.mongoSequence]
Tue Oct 28 02:33:33.701 Created collection restore_db.mongoSequence with options: { "create" : "mongoSequence" }
4 objects found
Tue Oct 28 02:33:33.701 Creating index: { key: { _id: 1 }, ns: "restore_db.mongoSequence", name: "_id_" }
Tue Oct 28 02:33:33.703 /tmp/jsons/orders.bson
Tue Oct 28 02:33:33.703 going into namespace [restore_db.orders]
Tue Oct 28 02:33:33.708 Created collection restore_db.orders with options: { "create" : "orders" }
461 objects found
Tue Oct 28 02:33:33.737 Creating index: { key: { _id: 1 }, ns: "restore_db.orders", name: "_id_" }
Tue Oct 28 02:33:33.851 /tmp/jsons/submittersLobs.bson
Tue Oct 28 02:33:33.851 going into namespace [restore_db.submittersLobs]
Tue Oct 28 02:33:33.856 Created collection restore_db.submittersLobs with options: { "create" : "submittersLobs" }
15 objects found
Tue Oct 28 02:33:33.857 Creating index: { key: { _id: 1 }, ns: "restore_db.submittersLobs", name: "_id_" }
Tue Oct 28 02:33:33.859 /tmp/jsons/lmrm__preferences.bson
Tue Oct 28 02:33:33.859 going into namespace [restore_db.lmrm__preferences]
1 objects found
Tue Oct 28 02:33:33.860 Creating index: { key: { _id: 1 }, ns: "restore_db.lmrm__preferences", name: "_id_" }
Tue Oct 28 02:33:33.865 /tmp/jsons/appConfigN.bson
Tue Oct 28 02:33:33.865 going into namespace [restore_db.appConfigN]
96 objects found
Tue Oct 28 02:33:33.869 Creating index: { key: { _id: 1 }, ns: "restore_db.appConfigN", name: "_id_" }
Tue Oct 28 02:33:33.888 Creating index: { key: { kind: 1, name: 1, to: 1 }, unique: true, ns: "restore_db.appConfigN", name: "PrimeIndex", dropDups: false, background: false }
Tue Oct 28 02:33:33.894 /tmp/jsons/errors.bson
Tue Oct 28 02:33:33.894 going into namespace [restore_db.errors]
47 objects found
Tue Oct 28 02:33:33.896 Creating index: { key: { _id: 1 }, ns: "restore_db.errors", name: "_id_" }
Tue Oct 28 02:33:33.907 /tmp/jsons/modelingTechnique.bson
Tue Oct 28 02:33:33.907 going into namespace [restore_db.modelingTechnique]
4 objects found
Tue Oct 28 02:33:33.908 Creating index: { key: { _id: 1 }, ns: "restore_db.modelingTechnique", name: "_id_" }
Tue Oct 28 02:33:33.913 /tmp/jsons/userAccess.bson
Tue Oct 28 02:33:33.913 going into namespace [restore_db.userAccess]
5 objects found
Tue Oct 28 02:33:33.914 Creating index: { key: { _id: 1 }, ns: "restore_db.userAccess", name: "_id_" }
Tue Oct 28 02:33:33.918 /tmp/jsons/ordersNew.bson
Tue Oct 28 02:33:33.918 going into namespace [restore_db.ordersNew]
Tue Oct 28 02:33:33.923 Created collection restore_db.ordersNew with options: { "create" : "ordersNew" }
6 objects found
Tue Oct 28 02:33:33.923 Creating index: { key: { _id: 1 }, ns: "restore_db.ordersNew", name: "_id_" }
Tue Oct 28 02:33:33.924 /tmp/jsons/system.users.bson
Tue Oct 28 02:33:33.924 going into namespace [restore_db.system.users]
1 objects found
Tue Oct 28 02:33:33.925 Creating index: { key: { _id: 1 }, ns: "restore_db.system.users", name: "_id_" }
Tue Oct 28 02:33:33.942 /tmp/jsons/appConfig.bson
Tue Oct 28 02:33:33.942 going into namespace [restore_db.appConfig]
1 objects found
Tue Oct 28 02:33:33.943 Creating index: { key: { _id: 1 }, ns: "restore_db.appConfig", name: "_id_" }
Tue Oct 28 02:33:33.948 /tmp/jsons/reports.bson
Tue Oct 28 02:33:33.948 going into namespace [restore_db.reports]
Tue Oct 28 02:33:33.948 restore_db.reports: skipping undefined field: capped
Tue Oct 28 02:33:33.948 restore_db.reports: skipping undefined field: size
Tue Oct 28 02:33:33.952 Created collection restore_db.reports with options: { "create" : "reports" }
4 objects found
Tue Oct 28 02:33:33.953 Creating index: { key: { _id: 1 }, ns: "restore_db.reports", name: "_id_" }
Tue Oct 28 02:33:33.954 /tmp/jsons/groups.bson
Tue Oct 28 02:33:33.954 going into namespace [restore_db.groups]
4 objects found
Tue Oct 28 02:33:33.955 Creating index: { key: { _id: 1 }, ns: "restore_db.groups", name: "_id_" }
[Lab root @ myhostname /opt/mongodb/bin]#
[Lab root @ myhostname /opt/mongodb/bin]# ./mongo --port 27020 -u adm -p pwd --authenticationDatabase admin
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> 
rs2:PRIMARY> show dbs
admin 0.203125GB
local 1.078125GB
restore_db 0.203125GB
test (empty)
rs2:PRIMARY> 
rs2:PRIMARY> 
rs2:PRIMARY> use restore_db
switched to db restore_db
rs2:PRIMARY> 
rs2:PRIMARY> show collections
appConfig
appConfigN
appConfigV
cart
errors
events
groups
lmrm__agg_pipeline_ttl
lmrm__audit_trail
lmrm__metadata2
lmrm__preferences
modelingTechnique
mongoSequence
orders
ordersNew
orders_auto
reports
submittersLobs
system.indexes
system.users
userAccess
userAccessTest
userRoles
rs2:PRIMARY> 
rs2:PRIMARY> 
rs2:PRIMARY> db.orders.findOne()
{
 "_id" : ObjectId("51ed952dad5aae282294be39"),
 "_class" : "com.test.testdata.iain.model.Order",
 "dataSets" : [
 "BILL_PYMT_REC",
 "ACTG_ENT"
 ],
 "oaAction" : "Assigned",
 "orderDescription" : "comments",
 "orderId" : "DAI108A",
 "orderStatus" : "Approved",
 "price" : 0,
 "proposalTitle" : "Detection",
 "reviewedDate" : ISODate("2013-07-23T15:51:32.168Z"),
 "reviewer" : {
 "_id" : 36050228,
 "soeid" : "st56973",
 "ritsid" : "1010356973",
 "name" : "srinivas mutyala",
 "emailaddress" : "srinivas.mutyala@company.com",
 "isenabled" : "Y"
 },
 "reviewerComments" : "Test Approve",
 "reviewerId" : "st56973",
 "reviewerName" : "srinivas",
 "submittedDate" : ISODate("2013-07-22T20:25:17.370Z"),
 "submitter" : {
 "_id" : 36050228,
 "soeid" : "ST56973",
 "ritsid" : "1010356973",
 "name" : "srinivas mutyala",
 "emailaddress" : "srinivas.mutyala@company.com",
 "isenabled" : "Y"
 },
 "submitterLob" : "DEOT - company Research",
 "proposalId" : 689,
 "proposalSector" : "DEMPT",
 "proposalLob" : "Global - Risk"
}
rs2:PRIMARY> 
rs2:PRIMARY> db.orders.count()
461
rs2:PRIMARY> 
rs2:PRIMARY> 
bye
[Lab root @ myhostname /opt/mongodb/bin]
mongoimport
Synopsis
The mongoimport tool provides a route to import content from a JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
Considerations
Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as MongoDB Backup Methods for this kind of functionality.
mongoimport is single-threaded and inserts one document at a time into MongoDB.
[Lab root @ myhostname /opt/mongodb/bin]# ./mongoimport --host myhostname --port 27020 -u adm -p pwd -d import_db -c orders --authenticationDatabase admin --file /tmp/jsons/orders.metadata.json 
connected to: myhostname:27020
Tue Oct 28 02:49:05.495 imported 1 objects
[Lab root @ myhostname /opt/mongodb/bin]# 
[Lab root @ myhostname /opt/mongodb/bin]# ./mongo --port 27020 -u adm -p pwd --authenticationDatabase admin
MongoDB shell version: 2.4.11
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> 
rs2:PRIMARY> show dbs
admin 0.203125GB
import_db 0.203125GB
local 1.078125GB
restore_db 0.203125GB
test (empty)
rs2:PRIMARY> 
rs2:PRIMARY> use import_db
switched to db import_db
rs2:PRIMARY> 
rs2:PRIMARY> show collections
orders
system.indexes
rs2:PRIMARY> 
rs2:PRIMARY> db.orders.count()
1
rs2:PRIMARY> db.orders.find()
{ "_id" : ObjectId("544f3c6156a38e8fdf248c9d"), "options" : { "create" : "orders" }, "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.orders", "name" : "_id_" } ] }
rs2:PRIMARY> 
rs2:PRIMARY>

  • Ask Question