Collections : MongoDB stores documents in collections. Collections are similer to tables in relational databases like Oraclle.
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
db.myNewCollection2.insert( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
Both the insert() and the createIndex() operations create their respective collection if they do not already exist.
Creating a collection using db.createCollection() method
MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.
Because MongoDB creates a collection implicitly when the collection is first referenced in a command, this method is used primarily for creating new collections that use specific options. For example, you use db.createCollection() to create a capped collection, or to create a new collection that uses document validation. db.createCollection() is also used to pre-allocate space for an ordinary collection.
Syntax:
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document> } )
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.
capped boolean Optional. To create a capped collection, specify true. If you specify true, you must also set a maximum size in the size field.
autoIndexId boolean Optional. Specify false to disable the automatic creation of an index on the _id field.
size number 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. it is optional
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.
usePowerOf2Sizes boolean Optional. Available for the MMAPv1 storage engine only.
noPadding boolean Optional. Available for the MMAPv1 storage engine only.
storageEngine document Optional. Available for the WiredTiger storage engine only.
New in version 3.0.
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 engin 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 Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation
validationLevel string Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update.
validationAction string Optional. Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted.
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
db.myNewCollection2.insert( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
Both the insert() and the createIndex() operations create their respective collection if they do not already exist.
Creating a collection using db.createCollection() method
MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.
Because MongoDB creates a collection implicitly when the collection is first referenced in a command, this method is used primarily for creating new collections that use specific options. For example, you use db.createCollection() to create a capped collection, or to create a new collection that uses document validation. db.createCollection() is also used to pre-allocate space for an ordinary collection.
Syntax:
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document> } )
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.
capped boolean Optional. To create a capped collection, specify true. If you specify true, you must also set a maximum size in the size field.
autoIndexId boolean Optional. Specify false to disable the automatic creation of an index on the _id field.
size number 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. it is optional
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.
usePowerOf2Sizes boolean Optional. Available for the MMAPv1 storage engine only.
noPadding boolean Optional. Available for the MMAPv1 storage engine only.
storageEngine document Optional. Available for the WiredTiger storage engine only.
New in version 3.0.
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 engin 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 Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation
validationLevel string Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update.
validationAction string Optional. Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted.
Examples
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
Create a Capped Collection
Capped collections have maximum size or document counts that prevent them from growing beyond maximum thresholds. All capped collections must specify a maximum size and may also specify a maximum document count. MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count. Consider the following example:
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
This command creates a collection named log with a maximum size of 5 megabytes and a maximum of 5000 documents.
The following command simply pre-allocates a 2-gigabyte, uncapped collection named people:
db.createCollection("people", { size: 2147483648 } )
No comments:
Post a Comment