Login to database
mongo
List available databases
show dbs
or
db.adminCommand( { listDatabases: 1 } )
Change active DB
use <db name>
List DB collections (tables)
show collections
List collection rows
db.<collectionName>.find()
or
db.<collectionName>.find().pretty()
Get documents count inside a collection
db.<collectionName>.count()
Bulk insert documents inside a collection
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
] );
Delete a document from a collection
db.<collectionName>.deleteOne( { "SystemName" : "Help" } )
db.<collectionName>.deleteMany({ })
Search for documents inside a collection
db.<collectionName>.find({id:'xxxx'})
Backup Database
mongodump -h sample.mongodbhost.com:27017 -d DATABASE_NAME -u USER_NAME -p SAMPLE_PASSWORD -o ~/Desktop
or
mongodump --uri "mongodb://usersname:password@127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" --out "C:\Umesh"
Restore Database
mongorestore --host sample.mongohost.com --port 27017 --username USER_NAME --password SAMPLE_PASSWORD --db DATABASE_NAME .
or
mongorestore mongodb+srv://UserID:Password@ServerIP/DBName Path2Dump
mongorestore --uri "mongodb://usersname:password@127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" -d dbname mongodbumppath
No comments:
Post a Comment