Database is a physical container of collections. Each database gets its own set of files in a file system. A single MongoDB server typically has multiple databases.
MongoDB is an open source, cross-platform, and document oriented database that provides high performance, high availability, and easy scalability in the realm of NoSQL databases. MongoDB works on the concept of collection and documents.
Collection is a group of MongoDB documents. It is the equivalent of tables in RDBMS. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are for similar or related purposes.
A document is a set of key-value pairs. Documents have dynamic schema, which means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.
The following table shows the relationship of RDBMS terminology with MongoDB.
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 / SQL*Plus | mongo |
mLab - mLab is a fully managed cloud database service that hosts MongoDB databases. It offers FREE services for the beginners to practice MongoDB queries. However, it charges additional cost if you need a Production-like environment.
1. Existing users can sign in with their login credentials and new users can sign up at mlab.com.
2. Once you have created an account at mlab.com, an email verification issent to your registered email id.
3. To complete the email verification process, click the verification link received at your registered email address. It will redirect you to mLab’s Home Page for confirmation and prompts you to select options for MongoDB Deployments and Environments.
4. Click the “+ Create new” button next to MongoDB Deployments. On the next page select “Google Cloud Platform” under Cloud Provider. Now, select “SANDBOX” under Plan Type, and then click Continue.
5. To proceed further enter “DATABASE NAME” under Final Details and then click “CONTINUE”.
6. To confirm the details, click “SUBMIT ORDER” under Order Confirmation.
7. Now, the new database is created under MongoDB Deployments.
8. Click the Database name and then “Users” tab and then Click “+Add database User” to create your desired login userid and password.
Once the user id is created, save the MongoDB host ID and port values provided on the Home page (highlighted below) to connect from shell or any user interface of MongoDB, like RoboMongo or Robo 3T (UI).
9. Once the user id is created for a database, Click the “Collection” tab and then Click “+Add Collection” to create a new collection called “ProductMaster”. To confirm the details, click “Create”
10. Install RoboMongo via Google (https://robomongo.org/download)
11. Once RoboMongo is installed, click “Create” in the MongoDB Connections that appears as a new window.
12. Once the connection window is displayed, user can try to connect to the MongoDB that was created earlier in mLab.
13. After entering all the connection details, click “Test” button to validate the connection details.
14. As the database is connected to expand the DB collections on the left side menuto find the “ProductMaster” collection that we created. Now, user can execute the command to “SELECT” rows[documents] from the “ProductMaster” collection and then click “Green Run” button b.getCollection('ProductMaster').find({})
15. Insert a row [document] in ‘ProductMaster” Collection by running the command mentioned below that is,
db.ProductMaster.insert({ ProductId: "100001", ProductName : "Bracelet", Size : "XL", Color : "Red"})
db.ProductMaster.insert({ ProductId: "100002", ProductName : "Watch",Color : "Brown"})
16. Now,user can see the two documents ready to be inserted into the collection.
Mongo DB Operation | Query |
---|---|
SELECT all rows from a Collection | db.getCollection('ProductMaster').find() |
Count no. of rows in a collection | db.ProductMaster.count() |
Find a specific product (WHERE) | db.getCollection('ProductMaster').find({ProductId: "100002"}) |
Find list of products (IN clause) | db.getCollection('productmaster').find({ProductId: {$in: ["100001","100002","100003"]}}) |
NOTE: The table and column names must be mentioned in right cases as MongoDB is case sensitive.