MongoDB – Installation

MongoDB Installation MongoDB is a NoSQL database server available for Most of the latest operating systems. Use one of the following tutorials to install MongoDB on your system. MongoDB Installation Follow below tutorials for the installation of MongoDB Install Mongodb on Ubuntu Install Mongodb on Debian Install Mongodb on CentOS & Fedora Windows users can download MongoDB installer from its official website.

Read More

MongoDB – Copy Database

MongoDB Copy Database Use copyDatabsae() function to copy MongoDB database from remote database instance to local database instance. You can also use this to copy database in the local instance as alternatives for rename database. Syntax: db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism) Examples: Copy Database on Same Instance You can create a duplicate database in same instance by using copyDatabase() function. db.copyDatabase(“olddb”,”newdb”) Output: { “ok” : 1 } Also you can use above option to rename database in MongoDB. But you must have enough space to create a copy of…

Read More

MongoDB – Rename Collection

MongoDB Rename Collection Use db.collection.renameCollection() method to rename existing collection in MongoDB database. Syntax: db.collection.renameCollection(target, dropTarget) Example: For example, You have a collection with wrong spelling “pproducts“. Let’s use the following command on mongo Shell to correct collection name to “products“. db.pproducts.renameCollection(“products”) Output: { “ok” : 1 }

Read More

MongoDB – count() method

MongoDB count() Method Use db.collection.count() method to count the number of documents available in a collection. Syntax: db.COLLECTION_NAME.count(query) Example – Count Total Documents Use the following command from mongo shell to count the number of available documents in users collection. > db.users.count(); Output: 8 Example – Count with Find You can also use count() function with other functions like find. > db.users.find({“user_name”:”rahul”}).count() Output: 1

Read More

MongoDB – mongo Shell

What is Mongo Shell? MongoDB provides an interactive mongo shell based on JavaScript. This provides a command line interface between users and MongoDB databases. You can find the best tutorials here for the installation of MongoDB server on Linux operating systems. What is mongo Shell? Mongo shell is a command line interface between user and database. You can connect to your MongoDB server using this shell and manage your databases and collections. You can also perform the administrative tasks on MongoDB. Connect to MongoDB Shell Type mongo on your system…

Read More

MongoDB – skip() method

MongoDB skip() Method Use skip() method to skip a number of documents. It works MongoDB find() method. Syntax: db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) Example For example you have 10 documents you collection users. Now get the 2 documents after 5 documents. Like you need to get 6’th and 7’th documents from the collection. db.users.find().limit(2).skip(5) The above query will skin first 5 documents from the collection and then show 2 documents. Output: { “_id” : ObjectId(“59a01c912427abe3470644de”), “id” : 1006, “user_name” : “peter”, “name” : [ { “first_name” : “Peter” }, { “middle_name” : “S” },…

Read More

MongoDB – limit() method

MongoDB limit() Method Use limit() method to show the limited number of documents in a collection with find() method. Syntax: db.COLLECTION_NAME.find().limit(NUMBER) Example: For example users collection have 19 documents. Now you need to get only first 2 documents from collection. > db.users.find().limit(2); Output: { “_id” : ObjectId(“59a01b102427abe3470644db”), “id” : 1001, “user_name” : “rahul”, “name” : [ { “first_name” : “Rahul” }, { “middle_name” : “” }, { “last_name” : “Kumar” } ], “email” : “[email protected]”, “designation” : “Founder and CEO”, “location” : “India” } { “_id” : ObjectId(“59a01b2f2427abe3470644dc”), “id” :…

Read More

MongoDB – Delete Document

MongoDB Delete Document Use db.colloction.remove() method to delete any document from MongoDB collection on basis of critaria. Syntax: > db.colloction.remove(CONDITION) Delete Matching Document Now use the following command deletes a specific document from MongoDB collection with user_name = rahul. > db.users.remove({“user_name”:”rahul”}) The above command will remove all document having user_name rahul. To remove only first matching document from collection use following command. > db.users.remove({“user_name”:”rahul”},1) Delete All Documents in Collection Use the following command with no deletion criteria to delete all documents from the specific collection. For example, below will delete…

Read More

MongoDB – Update Document

MongoDB Update Document Use db.collection.update() method to update document in MongoDB database. Syntax: > db.users.update(CONDITION, UPDATED DATA, OPTIONS) Find Current Document First find the current document in collection with user_name = rahul. We have already inserted documents in previous tutorial like below. > db.users.find().pretty({“user_name”:”rahul”}) { “_id” : ObjectId(“59a7aef2de55e8e213bf3f26”), “id” : 1001, “user_name” : “rahul”, “name” : [ { “first_name” : “Rahul” }, { “middle_name” : “” }, { “last_name” : “Kumar” } ], “email” : “[email protected]”, “designation” : “Founder and CEO”, “location” : “India” } Update Document Now change location…

Read More

MongoDB – Query Document

MongoDB Query Document Use db.collection.find() method to query collection for available documents. You can use show collections command to view available collection in your database. Syntax > db.COLLECTION_NAME.find(condition) Search All Documents Execute find() function on the collection without any condition to get all the available documents in collection. > db.users.find() You can also use pretty() function with above command to show formatted output. > db.users.find().pretty(); Output { “_id” : ObjectId(“59a01b102427abe3470644db”), “id” : 1001, “user_name” : “rahul”, “name” : [ { “first_name” : “Rahul” }, { “middle_name” : “” }, {…

Read More