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 Show
is a wrapper around the database command The method has the following prototype form: NoteStarting in MongoDB 4.2MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options 4, 5, 6 for
The method has the following parameters: Parameter Type Description
string The name of the collection to create. See
document Optional. Configuration options for creating a: Capped collection Custered collection View The 0 document contains the following fields:Field Type Description
boolean Optional. To create a , specify
string Required when creating a . The name of the field which contains the date in each time series document. Documents in a time series collection must have a valid BSON date as the value for the
string Optional. The name of the field which contains metadata in each time series document. The metadata in the specified field should be data that is used to label a unique series of documents. The metadata should rarely, if ever, change. The name of the specified field may not be
string Optional. Possible values are number Optional. Specifies the seconds after which documents in a or expire. MongoDB deletes expired documents automatically. For clustered collections, the documents are deleted automatically based on the clustered index key document Starting in MongoDB 5.3, you can create a collection with a . Collections created with a clustered index are called clustered collections. See
Field Description
Required. The clustered index key field. Must be set to
Required. Must be set to
Optional. A name that uniquely identifies the clustered index. New in version 5.3. document Optional. Starting in MongoDB 6.0, you can use to output the version of a document before and after changes (the document pre- and post-images): The pre-image is the document before it was replaced, updated, or deleted. There is no pre-image for an inserted document. The post-image is the document after it was inserted, replaced, or updated. There is no post-image for a deleted document. Enable 4 has the following syntax:
Description
Enables change stream pre- and post-images for a collection.
Disables change stream pre- and post-images for a collection. For complete examples with the change stream output, see For a example on this page, see New in version 6.0.
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
number Optional. The maximum number of documents allowed in the capped collection. The
document Optional. Available for the WiredTiger storage engine only. Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection. The value of the
Storage engine configuration specified when creating collections are validated and logged to the during replication to support replica sets with members that use different storage engines. TipSee also:
document Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Schema Validation. The To learn how to create a collection with schema validation, see
string Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update. Description No validation for inserts or updates. Default Apply validation rules to all inserts and all updates. Apply validation rules to inserts and to updates on existing valid documents. Do not apply rules to updates on existing invalid documents. To see an example that uses
string Optional. Determines whether to ImportantValidation of documents only applies to those documents as determined by the 8.To see an example that uses 4, see
document Optional. Allows users to specify a default configuration for indexes when creating a collection. The
Storage engine configuration specified when creating indexes are validated and logged to the during replication to support replica sets with members that use different storage engines.
string The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create. You must create views in the same database as the source collection. See also
array An array that consists of the . creates the view by applying the specified A view definition 4 cannot include the or the stage. This restriction also applies to embedded pipelines, such as pipelines used in or stages.The view definition is public; i.e. and 04 operations on the view will include the pipeline that defines the view. As such, avoid referring directly to sensitive fields and values in view definitions.See also
document Specifies the default for the collection. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option has the following syntax: When specifying collation, the 07 field is mandatory; all other collation fields are optional. For descriptions of the fields, see If you specify a collation at the collection level:
If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons. For a collection, you can only specify the collation during the collection creation. Once set, you cannot modify the collection's default collation. For an example, see
document Optional. A document that expresses the write concern for the operation. Omit to use the default write concern. When issued on a sharded cluster, converts the write concern of the command and its helper to Access ControlIf the deployment enforces , requires the following privileges: Task Required Privileges Create a non-capped collection on the database, or on the collection to create Create a for the collection on the database Create a on the database. However, if the user has the on the database and on the view to create, the user must also have the following additional permissions: on the source collection or view. on any other collections or views referenced in the A user with the built in role on the database has the required privileges to run the listed operations. Either create a user with the required role or BehaviorResource LockingChanged in version 4.2. obtains an exclusive lock on the specified collection or view for the duration of the operation. All subsequent operations on the collection must wait until releases the lock. typically holds this lock for a short time. Creating a view requires obtaining an additional exclusive lock on the 28 collection in the database. This lock blocks creation or modification of views in the database until the command completes.TransactionsChanged in version 4.4. Starting in MongoDB 4.4, you can create collections and indexes inside a if the transaction is not a cross-shard write transaction.To use in a transaction, the transaction must use read concern . If you specify a read concern level other than , the transaction fails. TipSee also:ExamplesCreate a Capped CollectionCapped 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:
This command creates a collection named 32 with a maximum size of 5 megabytes and a maximum of 5000 documents.See Capped Collections for more information about capped collections. Create a Time Series CollectionTo create a that captures weather data for the past 24 hours, issue this command:
Create a Clustered CollectionThe following example adds a named 34:
In the example, specifies:
Create a Collection with Change Stream Pre- and Post-Images for DocumentsStarting in MongoDB 6.0, you can use to output the version of a document before and after changes (the document pre- and post-images):
The following example creates a collection that has enabled:
Pre- and post-images are not available for a if the images were:
Additional considerations:
ImportantBackward-Incompatible FeatureStarting in MongoDB 6.0, if you are using document pre- and post-images for , you must disable for each collection using the command before you can downgrade to an earlier MongoDB version. TipSee also:
Specify CollationCollation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. You can specify at the collection or level. For example, the following operation creates a collection, specifying a collation for the collection (See for descriptions of the collation fields): 2This collation will be used by indexes and operations that support collation unless they explicitly specify a different collation. For example, insert the following documents into 65: 3The following operation uses the collection's collation: 4The operation returns documents in the following order: 5The same operation on a collection that uses simple binary collation (i.e. no specific collation set) returns documents in the following order: 6TipSee also:Specify Storage Engine OptionsYou can specify collection-specific storage engine configuration options when you create a collection with . Consider the following operation: Apa itu Collection dalam MongoDB?Collection MongoDb adalah tempat kumpulan informasi data yang berbentuk dokumen. Collection dipadankan seperti tabel-tabel yang berisi data pada database SQL. Document MongoDb adalah satuan unit terkecil dalam MongoDB.
Kapan harus menggunakan MongoDB?Kapan Menggunakan MongoDB
Misalnya kamu memiliki toko online yang ramai pelanggan yang setiap menitnya terdapat ada 100 data pelanggan yang masuk. Selain itu kamu juga bisa menggunakan MongoDB saat data yang didapatkan berkembang secara cepat dan memiliki struktur data yang kompleks.
Apakah MongoDB free?MongoDB adalah software sistem database yang bisa Anda gunakan gratis, baik untuk pengguna Windows, Linux atau macOS. Ini tentu pilihan yang cocok apabila Anda sedang mengerjakan project kecil atau pribadi dan tidak menyediakan anggaran khusus untuk membeli lisensi.
Mengapa MongoDB disebut database berorientasi dokumen?Berorientasi pada dokumen-Karena MongoDB adalah database tipe NoSQL, alih-alih memiliki data dalam format tipe relasional, MongoDB menyimpan data dalam dokumen. Ini membuat MongoDB sangat fleksibel dan mudah beradaptasi dengan situasi dan persyaratan dunia bisnis nyata.
|