Tag Archives: mongodb

MongoDB Commands Cheat Sheet for Beginners

mongodb cheat sheet for beginners

In this post, you will learn about MongoDB commands which could get you started and perform minimum database related activities such as create, update, drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as the cheat sheet. You may want to bookmark this page for quick reference. MongoDB Commands Cheatsheet The following is the list of the commands: Start and stop the MongoDB Database Access the MongoDB database using Shell Show all databases Create a database, say, testdb; Switch to the database Until a collection is created in a database, the database name is not listed as a result of execution of the command, “show dbs” Add a …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – Commands to Check MongoDB Performance on Linux

This blog represents code samples to check the MongoDB performance settings on Linux. Following configuration settings need to be checked: Filesystem as XFS Ulimit values Transparent huge pages Read ahead limit value File access time value TCP keepalive value Check Filesystem MongoDB recommends to use filesystem of type XFS when using WiredTiger as storage engine with MongoDB running on Linux. The details can be found on this page, MongoDB on Linux. Execute following command to check the filesystem: The output would look like following: Check ulimit values Check MongoDB recommended values for ulimit for number of open files and number of processes/threads allowed for the user access. The details can …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – How to Add Arbiter to Replica Set

This blog represents the instructions on how to add arbiter to existing Mongodb replica set. Arbiters are MongoDB instances whose primary role is to participate in replica set election in order to break ties and select PRIMARY.This instance do not hold any data and have minimal resource requirements. As a matter of fact, it does not need or require a dedicated hardware to run. However, it is advised to run arbiter on different server than the replica set. It can be any other server such as application server or monitoring server. Further details can be found on this page. Configure Arbiter Data Storage An arbiter does not store data. However, …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – Top 10 Best Practices for AWS Production Deployments

The following are some of the best practices which should be considered for your MongoDB production deployments on AWS. File system: MongoDB recommends using either of XFS or EXT4 filesystem for greater performance. With WiredTiger storage engine, it is strongly recommended to go with XFS file system. Refer this page, MongoDB production notes for finer details. AWS EBS (Elastic Block Store) Configuration EBS-optimized instances: It is advisable to use EBS optimized instances to host MongoDB database. With EBS optimized instances, there are separate network interfaces for database, and, other traffic (application traffic). In case, the replica set is configured with ephemeral storage, at least one of the secondaries in the …

Continue reading

Posted in AWS, MongoDB, NoSQL. Tagged with , , .

MongoDB – Not Master and SlaveOk Error Message

When trying to access MongoDB database collection data using command such as show collections on one of the secondary member of the replica set, error message such as not master and slaveOk=false occurs. Following screenshot represents the same: Following command when executed on secondaries resolves the above shown error: rs.slaveOk();

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – How to Enable Replication (Replica Set)

This blog represents steps required to enable replication in MongoDB when access control is disabled. The related details can be found on the page, Deploy Replica Set. Following are some of the key aspects which need to be understood in relation with MongoDB replica set: MongoDB replicaset consists of odd number of servers. This is done to achieve quorum in order to select a master server when one of the existing master server does down. Following are some of the steps which needed to be done to enable replication with MongoDB: Check MongoDB replication status Install additional Mongodb instances Start each member of replica set Initiate the Replica set Add …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – How to Reset Mongo Replica Set

The following instructions would help you to reset MongoDB replica set settings and start over with replication all over again. These are especially helpful for developers starting to learn MongoDB replication. Shut down the mongodb server Start the mongodb server in the standalone mode, e.g., without –replSet <replicaSetName>. Login to Mongodb; You can use admin database Make sure that user has readWrite permission to local database. In case, it is not, use following command to grant the role to the user. The following command gives readWrite permission on local database to admin user. Switch to local database Execute the command to make the cllection such as system.replset empty Make sure that …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – How to Create Database Users

First and foremost, one would be required to enable authentication with MongoDB database. The details can be found on this page MongoDB Database – Create User Command Following steps can be used to create a database user: Switch to the desired database. This is same as creating a database. Create user using following command: Above command would create user with with role such as dbOwner. The details on creating users can be found on this page covering the description on creating users – db.createUser() The user with role such as dbOwner can perform any administrative action on the database. The role combines the privileges granted by the readWrite, dbAdmin and …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – How to Enable Authentication on Mongo Database

Beginners starting with MongoDB would come across the requirement of enabling authentication on MongoDB database. This blog presents tips and code sample on how to enable login on MongoDB database such that one would require to pass their user credentials in order to access the database. In order to enable authentication for MongoDB databases, following can be done. This would essentially mean that users would have to provide user credentials in order to access a specific database. Otherwise, anyone can access any database using command such as mongo. Apply following changes in /etc/mongod.conf file. Note that mongod.conf is configuration file where other details such as data path can also be found. Start mongodb with –auth option. As …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB Command to Evaluate Query Performance

Following command can be used to evaluate query performance of a MongoDB Collection: The above command would print result such as following. COLLSCAN represents the collection scan. Query Performance Metrics Following are some of the metrics to watchout for when evaluating query performance: totalKeysExamined: Total number of index entries scanned totalDocsExamined: Total number of documents scanned to find the results. executionTimeMillis: Time required to excute the query

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – Evaluate Query Performance using Indexes

This blog represents commands used to manage MongoDB indexes on a particular collection and tips on how to evaluate query performance with or without indexes. Please feel free to suggest. Sorry for typos. Why create indexes? Indexes can significantly improve the read query performance for MongoDB collections. In absence of indexes, when searching for documents based on filter criteria, MongoDB performs collection scan, i.e., scan every document and returns the documents matching the filter criteria. This is not very efficient way of searching the document. In case of one or more fields which are frequently used for filtering out the document, it is recommended to create indexes on those fields. …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

Springboot MongoDB Repository – Code Example

This blog represents code required to create a Spring boot application that uses Spring Data MongoRepository interface to connect with MongoDB database. Step 1: Create a Springboot Maven project Create a new Spring Starter Project using Eclipse IDE. This would create a class annotated with @SpringBootAnnotation. Step 2: Include Spring Data Mongo support in pom.xml Step 3: Configure Mongoclient for database connectivity Create a Configuration class which is used to instantiate a MongoClient for connecting with MongoDB database. Step 3: Define MongoDB details in application.properties Step 4: Invoke MongoRepository instance Place following code in SpringBootAnnotation class for invoking MongoRepository instance (UserDAO in the code given below). In ideal scenario, you would want …

Continue reading

Posted in Java, MongoDB, NoSQL. Tagged with , , .

MongoDB – Top 10 Most Common Commands for Beginners

This article represents top 10 most commonly used commands for MongoDB beginners. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following is the list of these commands: Following is the list of these commands: Login into MongoDB: Following command can be used to login into MongoDB database for a particular database. Make sure that the user with credentials such as username and password exists in the database mentioned in place of dbname. Show all databases: Once logged in as a user with appropriate role as userAdmin or userAdminAnyDatabase, one can see all the databases using command such as …

Continue reading

Posted in MongoDB, NoSQL. Tagged with , .

MongoDB – Commands to Check the Status of MongoDB Database

This article represents different commands which can be used to check the status of MongoDB database on Linux/Ubuntu. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Note that mongod represents the daemon process of MongDB databass and, primarily, used to manage database access. Following are some of the commands which can be used to get the status of Mongodb: service mongod status: Displays the status of MongodB service as like the screenshot given below. systemctl status mongod: Displays the same status of MongoDB service as like above command as shown in figure 1. pgrep mongo: Prints the process ID …

Continue reading

Posted in NoSQL. Tagged with , .

MongoDB Commands & Concepts – Rookies Quick Reference

The article presents some of the basic concepts and commands which could prove useful for rookies starting with MongoDB. Key Concepts Simply speaking, MongoDB is a very popular NoSQL database with document-oriented storage. If you are a SQL developer and having challenges understanding document-oriented database, check out this page on mapping between SQL to MongoDB mapping. For a detailed introduction on MondoDB, check out this introduction page. Following are some of the key terminologies: Database Collections Document Field Primary key (_id) index JSON-styled Documents: The most important concept is document-oriented storage, and the documents are JSON-styled. Thus, one would require to learn JSON very well to do well with MongoDB …

Continue reading

Posted in NoSQL. Tagged with , .

Spring Data MongoDB Hello World with Spring MVC – Example

The article presents detailed steps on what is needed to get started with Spring Data MongoDB while you are working with Spring MVC web application. The article assumes that you have got Spring MVC application setup done. Step 1: Create Documents in MongoDB One could download MongoDB from http://www.mongodb.org/downloads page. Once downloaded, do the following to get started. Open a command prompt & goto bin folder found within MongoDB root folder. Before starting MongoDB server, create the data directory within root folder. Start the MongoDB server with command such as “mongod -dbpath <path-to-mongodb-root-folder>” Open another command prompt and goto bin folder. Execute “mongo” command and you are all set. Access …

Continue reading

Posted in Java, NoSQL. Tagged with , , .