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 – 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 – 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. Syntax > db.COLLECTION_NAME.find(condition) Search All Documents Execute find() function on the collection without any condition to get all the availabel 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” : “” }, { “last_name” : “Kumar” } ], “email” : “[email protected]”, “designation” : “Founder and CEO”,…

Read More

MongoDB – Insert Document

MongoDB Insert Document Use db.collection.insert() method to insert new document to MongoDB collection. You don’t need to create the collection first. Insert method will automatically create collection if not exists. Syntax > db.COLLECTION_NAME.insert(document) Insert Single Document Insert single ducument using the insert() method. It’s required a json format of document to pass as arguments. > db.users.insert({ “id”: 1001, “user_name”: “rahul”, “name”: [ {“first_name”: “Rahul”}, {“middle_name”: “”}, {“last_name”: “Kumar”} ], “email”: “[email protected]”, “designation”: “Founder and CEO”, “location”: “India” }) Output: WriteResult({ “nInserted” : 1 }) Insert Multiple Documents To insert multiple…

Read More

MongoDB – Show Collection

Mongodb Show Collection Use show collections command from MongoDB shell to list all collections created in the current database. First, select the database you want to view the collection. Mongodb Show Collection Select your database and run show collections command to list available collections in MongoDB database. > use mydb > show collections Output: TECADMIN accounts mycol pproducts users Get Collections Information To find detailed information about the database collections use .getCollectionInfos() function. This will provide an array of documents with collection information, such as name and options. > db.getCollectionInfos();…

Read More