Creating Collection/Table In MongoDB

In MongoDB Collection refers to table in RDBMS view. Before discussing the collection creation we need to know the difference between collection and cappedCollection.

>_ Collection Vs. CappedCollection

sno Collection CappedCollection
1 A group of documents A group of documents (one of the
flavor of collection)
2 Doesn’t have limitation on size It’s a fixed size collection.

A fixed-sized collection that automatically overwrites its oldest entries when it reaches its maximum size.

>_ How to Create Collections

We can create collection mainly in two ways

  1. Using db.createCollection() method
  2. Inserting document to a Collection which is not created yet.


db.createCollection(name, options) creates a new collection explicitly. In general MongoDB creates a collection implicitly when the collection is first referenced in a command

When to use db.createCollection()

  1. If we need a cappedCollection.
  2. If we want a collection with documentValidation(field level conditions like EmailId with @symbol, age as numeric, mobile number with x places etc..)

Basic Syntax of db.createCollection()
db.createCollection(name, options)
The db.createCollection() method has the following parameters:

Parameter Type Description
name string The name of the collection to create.
options document Optional. Configuration options for creating a capped collection or for preallocating space in a new collection.

Some of the main options are listed below

Field Type Description
capped boolean Optional. To create a capped collection, specify true. If you specifytrue, you must also set a maximum size in the size 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.
validator document Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation.
validationAction string Optional. Determines whether to error on invalid documents or justwarn about the violations but allow invalid documents to be inserted.

Example Collections :-

  • Collection Creation without any options
    > use TutorialToUs
      switched to db TutorialToUs
    >  db.createCollection("users")
      {    "ok" : 1.0 }					

  • CappedCollection Creation

    (Need to creation a collectionfor this purpose
    1. Which holds only 1 Document(row)
    2. If the new document came automatically need to overwrite.
    >use TutorialToUs
    switched to db TutorialToUs
    {    "ok" : 1.0}
    >show collections
    >db.tocuhlog.insert({filename:"~xyz.tmp",data:"Hey This is xyz.tmp file"})	
    Inserted 1 record(s) in 1ms
     "_id" : ObjectId("57739f54e2b90fd312b1c47b"),
      "filename" : "~xyz.tmp",
       "data" : "Hey This is xyz.tmp file"
    >db.tocuhlog.insert({filename:"newfile.tmp",data:"Hey I am New file"})	
    Inserted 1 record(s) in 1ms
        "_id" : ObjectId("57739fbae2b90fd312b1c47c"),
        "filename" : "newfile.tmp",
        "data" : "Hey I am New file"

    If you observe the above insert result old record (~xyz.tmp) is overwritten with new file due to capped concept.

    We will discuss insert() in next session clearly.

    Inserting document to a Collection which is not created yet:-
    MongoDB creates a collection implicitly when the collection is first referenced in a command from any insert() related methods. Means db.tempdoc.insert({snofield:1}) will create a collection named tempdoc if not exist.