MongoDB

5 Notes
+ Export data (Oct. 26, 2019, 3:16 p.m.)

Export a Collection to a JSON File: mongoexport --db mydb --collection posts --pretty --jsonArray --out posts.json --------------------------------------------------------------------- Export a Collection to a CSV File: mongoexport --db music --collection artists --type=csv --fields _id,artistname --out /data/dump/music/artists.csv --------------------------------------------------------------------- Export the results of a Query: mongoexport --db music --collection artists --query '{"artistname": "Miles Davis"}' --out /data/dump/music/miles_davis.json --------------------------------------------------------------------- The --limit Option mongoexport --db music --collection artists --limit 3 --out /data/dump/music/3_artists.json ---------------------------------------------------------------------

+ Commands (Oct. 26, 2019, 1:37 p.m.)

User the command "mongo" to access MongoDB shell, then use the following commands. ========================================= Create Database: use mydb db.students.insert({ id: 1 }) ----------------------------------------------------------------------- Show Databases: show dbs ----------------------------------------------------------------------- db.adminCommand( { listDatabases: 1 } ) The value (e.g. 1) does not affect the output of the command. ------------------------------------------------------------------------ db.adminCommand( { listDatabases: 1, nameOnly: true} ) db.adminCommand( { listDatabases: 1, filter: { "name": /^rep/ } } ) ------------------------------------------------------------------------ Display current selected database: db ------------------------------------------------------------------------ Delete Database: use mydb db.dropDatabase() ------------------------------------------------------------------------ Copy Database: db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism) Example: db.copyDatabase("olddb", "newdb") You can use above option to rename database in MongoDB. ------------------------------------------------------------------------ Copy Database from Remote Instance db.copyDatabase("remote_dbname", "local_dbname", "10.8.0.2", "username", "password") ------------------------------------------------------------------------ Create Collection: use mydb db.createCollection(name, options) db.createCollection("NAME") ------------------------------------------------------------------------ Show Collection: use mydb show collections ------------------------------------------------------------------------ Rename Collection: db.collection.renameCollection(target, dropTarget) db.pproducts.renameCollection("products") ------------------------------------------------------------------------ Drop Collection: db.COLLECTION_NAME.drop() db.students.drop(); db.getCollection("students").drop(); ------------------------------------------------------------------------ Insert Document: db.COLLECTION_NAME.insert(document) db.students.insert({ "id": 1001, "username": "mohsen.hassani", "name": [ {"first_name": "Mohsen"}, {"middle_name": ""}, {"last_name": "Hassani"} ], "email": "mohsen@mohsenhassani.com", "designation": "DevOps & Web Developer", "location": "Tehran/Iran" }) ------------------------------------------------------------------------ Insert Multiple Documents: var students = [{}] Pass a list of comma separated dictionaries (a JSON) db.students.insert(students); ------------------------------------------------------------------------ Query Document: db.COLLECTION_NAME.find(condition) Get all the availabel documents in collection: db.students.find() db.students.find().pretty(); ------------------------------------------------------------------------ Search Specific Documents: db.students.find({"id": 1001}) ------------------------------------------------------------------------ Update Document: db.students.update(CONDITION, UPDATED DATA, OPTIONS) db.students.update({"id": 1001}, {$set: {'location': 'Australia'}}) ------------------------------------------------------------------------ Delete Document: db.colloction.remove(CONDITION) Delete Matching Document: db.students.remove({"username": "mohsen.hassani"}) Remove all document having username mohsen.hassani. To remove only first matching document from collection: db.students.remove({"username": "mohsen.hassani"}, 1) Delete All Documents in Collection: db.students.remove({}) ------------------------------------------------------------------------ limit() Method: Use limit() method to show the limited number of documents in a collection with find() method. db.COLLECTION_NAME.find().limit(NUMBER) db.students.find().limit(2); ------------------------------------------------------------------------ sort() method: db.COLLECTION_NAME.find().sort({KEY:type}) Ascending Order: db.students.find().sort({username:1}); Descending Order: db.students.find().sort({username:1}); ------------------------------------------------------------------------ count() Method: db.COLLECTION_NAME.count(query) db.students.count(); Count with Find: db.students.find({"username": "mohsen.hassani"}).count() ------------------------------------------------------------------------

+ Terminology (Oct. 26, 2019, 1:32 p.m.)

RDBMS MongoDB Database Database Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by mongodb itself) Database Server and Client Mysqld/Oracle mongod mysql/sqlplus mongo

+ Service & Logs (Oct. 26, 2019, 10:45 a.m.)

sudo service mongod start ------------------------------------------------------------ /var/log/mongodb/mongod.log [initandlisten] waiting for connections on port 27017 27017 is the default port the standalone mongod listens on. ------------------------------------------------------------

+ Installation (Oct. 26, 2019, 10:44 a.m.)

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/ ---------------------------------------------------------- The unofficial mongodb package provided by Debian is not maintained by MongoDB and conflict with MongoDB’s offically supported packages. Use the official MongoDB mongodb-org packages, which are kept up-to-date with the most recent major and minor MongoDB releases. To check if Debian’s mongodb package is installed on the system, run sudo apt list --installed | grep mongodb. You can use sudo apt remove mongodb and sudo apt purge mongodb to remove and purge the mongodb package before attempting this procedure. ---------------------------------------------------------- Installation: 1- wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - 2- echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list 3- apt update 4- apt install mongodb-org ----------------------------------------------------------