MongoDB - how to Create Database and Collection using Robo 3T - TechDB

Latest

All about Database Programming, Performance Tuning and Best Practices.

BANNER 728X90

Tuesday, 17 December 2019

MongoDB - how to Create Database and Collection using Robo 3T

MongoDB - how to Create Database and Collection using Robo 3T


How to create Database and collection in Robo 3T


There are two ways to create Database and Collection
1)    Using Robo 3T Shell or Command prompt
2)    Using Robo 3T – GUI Menu option
Before going to execute all this first we have to connect the mongo server. Just open the Robo 3T application and connect the server.
Step to create Database using Robo 3T GUI Menu option.

1)    Right click on Connection and Select Create Database
Click Create

You can see new Database “TechDB” created. 
Step to create Database using Robo 3T application shell.

1)    Right click on Connection and Select Open Shell.
Type use TechDB in shell and press F5
Database created but you cannot see in left side tree after refresh also. When you create Database using Shell then without any collection created inside the Database it will not show the database name.
Collection is equivalent a Table in RDBMS. The basic systax for Collection is
db.createCollection(name, options)
name - The name of the collection to create
options - Optional. Configuration options for creating a capped collection, for preallocating space in a new collection
Options parameter is optional. But you can use below parameters. MongoDB check these parameters before inserting any documents in collection.
Field Type Description
capped boolean Optional. To create a capped collection, specify true. If you specify true, you must also set a maximum size in the size field.
autoIndexId boolean Optional. Specify false to disable the automatic creation of an index on the _id field.
size number Optional. Specify a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The size field is required for capped collections and ignored for other collections.
max number Optional. The maximum number of documents allowed in the capped collection. The size limit takes precedence over this limit. If a capped collection reaches the size limit before it reaches the maximum number of documents, MongoDB removes old documents. If you prefer to use the max limit, ensure that the size limit, which is required for a capped collection, is sufficient to contain the maximum number of documents.
Capped collections have maximum size or document counts that prevent them from growing beyond maximum thresholds. All capped collections must specify a maximum size and may also specify a maximum document count. MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

Example:

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

This command creates a collection named log with a maximum size of 5 megabytes and a maximum of 5000 documents. 
Create collection without capping

Lets create a collection in the Database
Create a “User” collection using inbuild method createCollection

db.createCollection(“user”)

db – Refer to Database “TechDB”. Before executing any command, you must be sure that you are working on correct database by using db command.
Now you can see the new Database “TechDB” and also one collection “user” created. You can create multiple collections in a single Database. And each collection holds multiple Documents.
You can also create Collection using Robo 3T GUI

Right Click on Collection and Select Create Collection
In next window you can provide a Collection name and then click Advance for Capping options. Capped collection is Optional.
Click Create
DROP Collection using drop() method.
db.collection_name.drop()

db.user.drop()
db – Refer to Database “TechDB”.
DROP Database using dropDatabase() method.

db.dropDatabase()
Refresh the connection or type show dbs command in shell



No comments:

Post a Comment