Create Collection in MongoDB


db.createCollection(<name>, <options>) method is used by MongoDB to create collection.
  • <name> parameter is string type; here we put the name of the collection.
  • <Options> parameter is a document that specifies options about memory size and indexing etc. The options parameter is optional, so you need to specify the only the <name> of the collection. Following is the list of options you can use:


Field

Type

Description

capped
Boolean
 The capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify the size parameter also.

 autoIndexID
Boolean
If true, automatically create an index on the _id field.s Default

size
number
If capped is true it specifies a maximum size in bytes for a capped collection, and then you need to specify this field also.

max
number
This allows the maximum number of documents allowed in the capped collection.


While inserting the document, MongoDB first checks the size field of capped collection, then it checks the max field.

Example:
Following I am trying to write the basic syntax for createCollection() method.

>use test
switched to db test
>db.createCollection("mongocollection")
{ "ok" : 1 }

You can check the created collection by using the command show collections

>show collections
mongocollection
system.indexes

Following example shows the syntax of createCollection() method with few important options:

>db.createCollection("mongocollection ", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }

In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.

>db.Codefari.insert({"name" : "Codefari"})
>show collections
mongocollection
system.indexes
codefari

Drop Database in MongoDB


The dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing database.

EXAMPLE:
By using the command ‘show dbs’ check the available list of databases


>show dbs
local      0.78234GB
myMongodb       0.343012GB
test       0.43012GB

If you want to delete new database <myMongodb>, then use dropDatabase() command as below.


>use myMongodb
switched to db myMongodb
>db.dropDatabase()
>{ "dropped" : " myMongodb ", "ok" : 1 }

Now checklist of databases


>show dbs
local      0.78234GB
test       0. 43012GB

Note: This will delete the selected database. If you have not selected any database, then it will delete the default 'test' database

Create Database in MongoDB


Using the ‘Use’ keyword we can create a database in MongoDB, for example, see the following command. 

>use myMongoDB

Command will create a new database if “myMongoDB” does not exist otherwise it will return the existing database.
To check your currently selected database use the command DB

>db
myMongoDB

If you want to check your database list, then use the command "show dbs".

>show dbs
Local 0.78987GB
myMongoDB 0.98978GB

How to Install MongoDB on Windows


You can download the latest release of MongoDB from http://www.mongodb.org/downloads, Make sure you get the correct version of MongoDB depending upon your windows version.
32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.
Now extract your downloaded file in any drive suppose we extract it in c:\\ drive, after extract file you will see a folder “mongodb-win32-x86_64-[version]” Here [version] is the version of MongoDB downloaded. You can rename this file name.
Now open the command prompt and run the following command.

[Your mongodb file location]\bin mongod 
 for example  C: \MongoDB\bin mongod

MongoDB require the folder “data\db” inside MongoDb folder location, the default location for the MongoDB data directory is c:\ MongoDB \data\db.
If you have installed the MongoDB at different locations, then you need to specify an alternate path for \data\db by setting the path dbpath in mongod.exe. For the same issue following commands
In command prompt navigate to the bin directory present into the MongoDB installation folder. Suppose my installation folder is D:\set up\mongodb.

C:\Users\XYZ>d:
D:\>cd "set up"
D:\set up>cd mongodb\bin
D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"

If everything going well it will show a message “Waiting for Connection”. Now open a new command prompt and run the following command.

D:\set up\mongodb\bin>mongo.exe
MongoDB shell version: 2.4.7
Use test
>db.test.save( { a: 1 } )
>db.test.find()
{ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }

This will show that MongoDB is installed and run successfully. Next time when you run MongoDB you need to issue only commands.

D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"
D:\set up\mongodb\bin>mongo.exe

Note: 32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...