MongoDB Collection
Collection in MongoDB is an analog of a table in a relational database. It is a group of documents. Each document in a collection can have different structures. A collection can contain up to 64 indexes.
MongoDB Create Collection
MongoDB createCollection() method is used to create a collection in mongodb.
Syntax of createCollection()
db.createCollection(name, options)
Here, name is the name of the collection and of string type and options is an optional parameter.
The options parameter can be the following -
Option | Type | Description |
capped | boolean | Specify true to create capped collection. |
autoIndexId | boolean | Specify false to disable the automatic creation of an index. |
size | number | Specify a maximum size in bytes for a capped collection. |
max | number | The maximum number of documents allowed in the capped collection. |
storageEngine | document | Available for the WiredTiger storage engine only. |
validator | document | Allow users to specify validation rules or expressions for the collection. |
viewOn | string | The name of the source collection or view from which to create the view. |
pipeline | array | An array that consists of the aggregation pipeline stage. |
collation | document | Specifies the default collation for the collection. |
writeConcern | document | A document that expresses the write concern for the operation. |
Example
db.createCollection("students", { capped : true, size : 5242880, max : 3000 } )
![mongodb create collection](/images/mongodb/createcollection.png)
We can also create collection when documents are first inserted. This is the dynamic behaviour of MongoDB. Like, in this example, we need to store a document in a collection 'employee'. So before creating this, we can directly insert the document in 'employee' collection.
> use company;
switched to db company
> db.employee.insert({name:"Priska",dept:"IT",role:"developer"});
WriteResult({ "nInserted" : 1 })
> db.employee.find();
{ "_id" : ObjectId("5d414003092ef252dc155a6f"), "name" : "Priska", "dept" : "IT", "role" : "developer" }
>
MongoDB List Collections
MongoDB show collections display a list of all defined collections on the current selected database.
In the below command, we have selected 'school' database and listed all its collections.
> use school;
switched to db school
> show collections
attendence
marks
students
>
MongoDB Collection Statistics
We can know about individual collection statistics using stats() method. This is helpful in complicated debugging.
> db.attendence.stats()
{
"ns" : "school.attendence",
"size" : 53,
"count" : 1,
"avgObjSize" : 53,
"storageSize" : 16384,
"capped" : false,
............
...........
MongoDB Count Document
To count number of documents in a collection, simply write the following command -
> db.students.count();
5
Size of Collection
The dataSize() method returns the size of the given collection.
> db.students.dataSize();
345
Size of Document in Collection
The storageSize() method returns the total size of documents stored in the collection.
> db.students.storageSize();
36864
Size of all indexes in a Collection
The totalIndexSize() method returns the total size of all indexes in the given collection.
> db.students.totalIndexSize();
36864
Collection Latency Statistics
MongoDB latencyStats() method returns latency statistics for read requests, write requests and database commands for the given collection.
db.attendence.latencyStats().pretty()
{
"ns" : "school.students",
"host" : "DESKTOP-NAME",
"localTime" : ISODate("2019-08-02T11:03:48.966Z"),
"latencyStats" : {
"reads" : {
"latency" : NumberLong(193999),
"ops" : NumberLong(3)
},
"writes" : {
"latency" : NumberLong(0),
"ops" : NumberLong(0)
},
"commands" : {
"latency" : NumberLong(242811),
"ops" : NumberLong(3)
},
"transactions" : {
"latency" : NumberLong(0),
"ops" : NumberLong(0)
}
}
}
MongoDB Rename Collection
MongoDB provides renameCollection() method to rename the existing collection.
db.students.renameCollection("undergrad");
{ "ok" : 1 }
MongoDB Delete Collection
MongoDB drop() method is used to delete the existing collection. It returns true on success.
> db.marks.drop();
true