sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install mongodb-orgMongoDB Tools contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongorestore, mongostat, and mongotop.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install mongodb-org-toolsImportant: Avoid using
mongoimportandmongoexportfor full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Usemongodumpandmongorestoreas described in MongoDB Backup Methods for this kind of functionality.
Example command:
mongoexport -h <hostname>:<port> -u <username> --db=<your_db> --collection=<your_collection> --out=<output.csv> -q '{storeId:12}' --authenticationDatabase=<your_db> --type csv --fields order,'customer.id','customer.e mail',storeId -p <password>Note:
- If your account,
<username>and<password>, is granted for a specifice database,<your_db>, then--authenticationDatabaseshould be--authenticationDatabase=<your_db>.
With MongoDB 3.6
use admin
db.createUser({
user: "user-write",
pwd: "user-write-password",
customData: {
createdBy: "minh.gdd"
},
roles: [
{
role: "readWrite",
db: "dbname"
}
]
})use admin
db.createUser({
user: "user-read",
pwd: "user-read-password",
customData: {
createdBy: "minh.gdd"
},
roles: [
{
role: "read",
db: "dbname"
}
]
})mongodump --host=<hostname> --port=27017 --archive=output.archive --username=uname --password=pw --authenticationDatabase=admin --authenticationMechanism="SCRAM-SHA-1" --db=dbname --collection=cname
mongorestore --host=<hostname> --port=27017 --archive=input.archive --username=uname --password=pw --authenticationDatabase=admin --authenticationMechanism="SCRAM-SHA-1"
mongoexport -h <hostname> --port 27017 -u uname -p pw --db dbname --collection cname --authenticationDatabase dbname --authenticationMechanism "SCRAM-SHA-1" --out orders.json
mongoimport -h <hostname> --port 27017 -u uname -p pw --db dbname --collection cname --authenticationDatabase dbname --authenticationMechanism "SCRAM-SHA-1" --file input.json