Mongodb Create Table Collection - MongoDB Tutorial



Mongodb Create Table Collection

MongoDb
  • In mongoDB all the data are stored in document oriented types called as “collection”.
  • A collection is a group of related documents that have a set of shared common indexes.
  • Collections are similar to table in relational databases(RDBMS).

Creating Collection in MongoDB

  • In mongoDB , collections are created very easily.
  • It is similar to create table in RDBMS, the tables hold the user data.
  • But in mongoDB we create collection being used for storing multiple documents (or) records.

Syntax (Show database) :

db.createCollection(name, options)
  • Here in this syntax the parameter “name” represent the name of the collection.
  • And the second parameter is optional and used to specify configuration of the collection, i.e. for example it is used to specify the memory sizing & indexing.
  • The options document creates a capped collection that is, it preallocates space in a new ordinary collection, or specifies document validation criteria.
Use <database>

The options document contains the following fields :

Field Type Description
capped boolean
  • To create a capped collection, we need to specify true in parallel setting the maximum size in the size field.
autoIndexId boolean
  • Specifying false to disable the automatic creation of an index on the _id field.
  • Note : For replica sets, do not set autoIndexId to false.
size number
  • Specifying 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 field size is required for capped collections and ignored for other collections.
max number
  • 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.
usePowerOf2Sizes boolean
  • Available for the MMAPv1 storage engine only. (MMAPv1 is MongoDB's original storage engine based on memory mapped files)
noPadding boolean
  • Available for the MMAPv1 storage engine only.
  • With noPadding flag set to be true, the allocation strategy does not include additional space to accommodate document growth, as such, document growth will result in new allocation.
  • Use for collections with workloads that are insert-only or in-place updates (such as incrementing counters). Defaults to false.
storageEngine document
  • Available for the WiredTiger storage engine only. (WiredTiger storage engine is the default storage engine starting in MongoDB 3.2)
  • Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection.
  • The value of the storageEngine option should take the following form:
  • { <storage-engine-name>: <options> }
  • Storage engine configuration specified when creating collections are validated and logged to the oplog during replication to support replica sets with members that use different storage engines.
validator document
  • Allows users to specify validation rules or expressions for the collection.
  • You can specify the expressions using the same operators as the query operators with the exception of $geoNear, $near, $nearSphere, $text, and $where.
  • Note :
    • Validation occurs during updates and inserts. Existing documents do not undergo validation checks until modification.
    • You cannot specify a validator for collections in the admin, local, and config databases.
    • You cannot specify a validator for system.* collections.
validationLevel string
  • Determines how strictly MongoDB applies the validation rules to existing documents during an update.
validationLevel Description
"off" No validation for inserts or updates.
"strict" Default Apply validation rules to all inserts and all updates.
"moderate" Apply validation rules to inserts and to updates on existing valid documents. Do not apply rules to updates on existing invalid documents.
validationAction string
  • Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted.
  • Note: Validation of documents only applies to those documents as determined by the validation Level.
validationAction Description
"error" Default Documents must pass validation before the write occurs. Otherwise, the write operation fails.
"warn" Documents do not have to pass validation. If the document fails validation, the write operation logs the validation failure.
"moderate" Apply validation rules to inserts and to updates on existing valid documents. Do not apply rules to updates on existing invalid documents.
indexOptionDefaults document
  • Allows users to specify a default configuration for indexes when creating a collection.
  • The indexOptionDefaults option accepts a storageEngine document, which should take the following form:
  • { <storage-engine-name>: <options> }
  • Storage engine configuration specified when creating indexes are validated and logged to the oplog during replication to support replica sets with members that use different storage engines.

Sample Query 1

db               --display the created database
db.createCollection("mongodb_table")   -- created collection

Output

MongoDb
  1. In this statement we display the database.
  2. Here in this statement we create a collection using “db.createCollection("mongodb_table")” rich query and have created successfully the identified set value as “1”.

Sample Query 2

db.createCollection(“newCollection2”, { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )

Output

MongoDb
  1. Here in this statement the maximum number of documents allowed in the capped collection : 10000 (rows)and specify the maximum size in bytes for a capped collection : 6142800 bytes.

Related Searches to Mongodb Create Table Collection - MongoDB Tutorial