MongoDB - SPLessons

MongoDB Collections

Chapter 12

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Collections

MongoDB Collections

Create Collections

shape Description

MongoDB Collections can be implicitly created in MongoDB where these collection are initially allusion in a command, and these strategy where utilized fundamentally to create new MongoDB collections that are used for creating some particular options.

For instance, using db.createCollection() to make another collection or to make a capped collection that are used for reports acceptance. And also one can create an additional db.createCollection() from the space that are pre-allotted for a common collections.

shape Syntax

The syntax for creating a new  collections is as follows:
db.createCollection(name, options)

Name => Name of the collection that as to be created.
Options => Is a report utilised for a collections that has to be configured.

shape Examples

The below example describes how to create a new MongoDB Collections.

MongoDB shell version: 3.2.7
connecting to: test
> show dbs
local  0.000GB
mydb   0.000GB
> use admin
switched to db admin
> show collections
> db.createCollection("mycollection")
{ "ok" : 1 }
> show collections
mycollection
> db.createCollection("mycol",{capped:true,autoIndexID:true,size:6142800,max:100
00})
{ "ok" : 1 }
> db.james.insert({"name":"james"})
WriteResult({ "nInserted" : 1 })
> show collections
james
mycol
mycollection

Here in the above example, a new MongoDB Collections James has been successfully created.

Drop Collections

shape Description

Drop collections are used for dropping a MongoDB Collections from the database.

shape Syntax

The syntax for dropping a collections is as follows:
db.Collection_Name.drop()

Name => Name of the collection that as to be deleted.
Drop =>Dropping a collection from the database.

shape Examples

The below example describes how to drop a collections from the database.

MongoDB shell version: 3.2.7
connecting to: test
> show dbs
local  0.000GB
mydb   0.000GB
> use admin
switched to db admin
> show collections
> db.createCollection("mycollection")
{ "ok" : 1 }
> show collections
mycollection
> db.createCollection("mycol",{capped:true,autoIndexID:true,size:6142800,max:100
00})
{ "ok" : 1 }
> db.james.insert({"name":"james"})
WriteResult({ "nInserted" : 1 })
> show collections
james
mycol
mycollection
> db.mycollection.drop()
true
> show collections
james
mycol
>

Here in the above example, my collections has been successfully dropped from the database.

Summary

shape Key Points

  • MongoDB Create Collection – Is used to create new collections from the database.
  • MongoDB Drop Collection – Is used to drop a collection from the database.