In this blog we will learn about some terms and crud operations in mongodb. As we are very much aware about the terms related with RDBMS such as creating database, creating tables and insert records in the table. Here we will learn about the basic terms in mongodb such as “collections” is similar to tables in mongdb, documents are similar to records in mongodb. Here first we will see how to create a database, collection and documents in mongodb.
First let us start the mongo shell in our terminal:
MongoDB shell version: 3.0.12
connecting to: test
Now if we want to check all the database on the server, we use the following command on mongo shell:
> show dbs
The above command will give us the list of database
Now we can select the name of the db in which we want to work.
Output: switched to db <databasename>
If we want to create a new database just use the above command with the database name that we want.
Output: switched to db demodb
Now let us learn how to create collections in the database. In MongoDB collections are created implicitly when we first use the collection name in a command. It pre-allocates space for a collection. For creating a capped collection we need to create a capped collection explicitly using the mong shell helper db.createCollection().Below is the prototype for db.createCollection()
Below are the meanings for the option in the prototype
Name : name for the collection
Capped: it tells about the collection. That the
collection is of fixed size which will automatically overwrite its
oldest entries on reaching its max size.If option capped is choosen
as true, it is necessary to specify size parameter.
AutoIndexid: if this option is set to true than
is creates index automatically to _id field will be created
Size : size of the document
Max : It shows that maximum number of documents
(i.e. records) that we can store in the collection
Let us create a collection with different specifications as below: