Hi in this tutorial we will discuss about the creating databases and collections in the MongoDB. Actually there is no command to create database in the MongoDB, it created automatically when will try to insert data in to the database name.
MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will create it on the fly, during the first time you save the value into the defined collection (or table in SQL), and database.
For developer from SQL background, we need to create a database, table and insert values into table manually. In MongoDB, you don’t need to mention what you want to create, when first time you save the value into the defined collection (table), under selected database, MangoDB will create the value, collection and database automatically.
Note :
MongoDB contains “db.createCollection()” to create collection manually, but Not database.
Databases:
A number of databases can be run on a single MongoDB server. Default database of MongoDB is “db“, which is stored within data folder.
MongoDB can create databases on the fly. It is not required to create a database before you start working with it.
“show dbs” command provides you with a list of all the databases.
1. Show all database-
Open the Command prompt and go to the c:/cd mongodb/bin and press enter and run the c:/mongodb/bin> mongod and other c:/mongodb/bin> mongo
> show dbs dineshonjavaDB 0.203125GB local (empty) mydb 0.203125GB >
Here show all databases list.
dineshonjavaDB
local
mydb
Run ‘db’ command to refer to the current database object or connection.
> db dineshonjavaDB >
To connect to a particular database, run use command.
> use dineshonjavaDB switched to db dineshonjavaDB
Database names can be almost any character in the ASCII range. But they can’t contain an empty string, a dot (i.e. “.”) or ” “. Since it is reserved, “system” can’t be used as a database name. A database name can contain “$”.
Documents-
document is the unit of storing data in a MongoDB database. document use JSON (JavaScript Object Notation, is a lightweight, thoroughly explorable format used to interchange data between various applications) style for storing data.
A simple example of a JSON document is as follows :
{ empName: “Dinesh” }
Often, the term “object” is used to refer a document.
Documents are analogous to the records of a RDBMS. Insert, update and delete operations can be performed on a collection. The following table will help you to understand the concept more easily :
RDBMS | MongoDB |
---|---|
Table | Collection |
Column | Key |
Value | Value |
Records / Rows | Document / Object |
Collections-
A collection may store number of documents. A collection is analogous to a table of a RDBMS.
A collection may store documents those who are not same in structure. This is possible because MongoDB is a Schema-free database. In a relational database like MySQL, a schema defines the organization / structure of data in database. MongoDB does not require such a set of formula defining structure of data. So, it is quite possible to store documents of varying structures in a collection. Practically, you don’t need to define a column and it’s datatype unlike in RDBMS, while working with MongoDB.
In the following code, it is shown that two MongoDB documents, belongs to same collection, storing data of different structures.
{“empName” : “Dinesh”} {“salary” : 70000}
A collection is created, when the first document is inserted.
Valid collection names-
Collection names must begin with letters or an underscore.
A Collection name may contain numbers.
You can’t use “$” character within the name of a collection. “$” is reserved.
A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90 characters.
Using a “.” (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don’t have much to do with it.
Following is how to use it programmatically :
db.employees.dav.findOne()
capped collections-
Imagine that you want to log the activities happening with application. you want to store data in the same order it is inserted. MongoDB offers Capped collections for doing so.
Capped collections are collections which can store data in the same order it is inserted.
It is very fixed size, high-performance and “auto-FIFO age-Out”. That is, when the allotted space is fully utilized, newly added objects (documents) will replace the older ones in the same order it is inserted.
Since data is stored in the natural order, that is the order it is inserted, while retrieving data, no ordering is required, unless you want to reverse the order.
New objects can be inserted into a capped collection.
Existing objects can be updated.
But you can’t remove an individual object from the capped collection. Using drop command, you have to remove all the documents. After drop, you have to recreate the capped collection.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…