The tutorial provides the steps to create a collection and drop a collection using MongoDB. A collection is created in mongodb using createCollection()
Document: A document is single unit or data record similar to a row in RDBMS for storing information. A document is a JOSN object storing data in the form of key-value pairs.
Collection: A collection is a group of documents for storing data similar to RDBMS tables. MongoDB contains multiple collections in the database similar to RDBMS which contains multiple tables in the database
Create Collection in MongoDB
The MongoDB provides the method – db.createCollection(name, options) to create a collection.
Create Collection Syntax:
db.createCollection(name, options)
Where
name is the collection name
options is the document to specify collection configuration
Argument | Argument Type | Argument Description |
Name | String | Name of the Collection |
Options | Document | Optional. Configuration details like Memory Size, Indexing |
capped | Boolean |
Optional. Enables the Capped Collection which are fixed size collections. When reached max size (in bytes) , capped collection overwrites the old entries |
autoIndexId | Boolean |
Optional. When enabled , creates index on _id field. |
size | Number |
Optional. Used in Capped Collection . When enabled, specify max size in bytes |
max | Number |
Optional. When Enabled, allows to specify max documents in the Capped Collection. |
Lets execute a simple collection method call for creating the collection in MongoDB
> use oracleappshelpDB switched to db oracleappshelpDB > db.createCollection("oracleappsHelpBCollection") { "ok" : 1 }
The OK depicts that the collection – “oracleappsHelpBCollection” is created. We can verify by executing the show collections
> show collections oracleappsHelpBCollection >
Let’s execute create collection method with the Optional arguments
> db.createCollection("oracleappscol", { capped : true, size : 10240, max : 1000 } ) { "ok" : 0, "errmsg" : "16: Resource device", "code" : 8, "codeName" : "UnknownError" } >
Note that we do not need to create collections in MongoDB as it automatically creates the collection when an document is inserted.
The below MongoDB Insert Document command shows the created collections.
> db.oracleappshelpDB.insert({"studentName" : "Mohit Sharma"}) WriteResult({ "nInserted" : 1 }) > show collections oracleappsHelpBCollection oracleappshelpDB >
Drop collection in MongoDB
The MongoDB provides the method – db.collection.drop() to drop a collection.
The MongoDB provides the method – db.collection.drop(name, options) for dropping the collections.
Drop Collection Syntax:
db.Collection.drop()
Where
Collection is the name of the collection.
The below command show collections list down the available collections for the Database – oracleappshelpDB
> show collections oracleappsHelpBCollection oracleappshelpDB >
Now , drop the collection – oracleappshelpDB from the database by executing the below command
> db.oracleappshelpDB.drop() true >