Updated on 2022-06-21 GMT+08:00

Creating and Managing Collections

For details about the rules of the write/update and delete commands, see Write/Update and Delete.

Creating a Collection

  1. Run db.createCollection(name, options) to create a collection.

    db.createCollection(<name>, { capped: <boolean>,
                                  autoIndexId: <boolean>,
                                  size: <number>,
                                  max: <number>,
                                  storageEngine: <document>,
                                  validator: <document>,
                                  validationLevel: <string>,
                                  validationAction: <string>,
                                  indexOptionDefaults: <document>,
                                  viewOn: <string>,
                                  pipeline: <pipeline>,
                                  collation: <document>,
                                  writeConcern: <document>} 

    Table 1 Parameter description

    Field

    Type

    Description

    apped

    boolean

    Optional. If a capped collection needs to be created, the value is true. If the value is true, the size field needs to be specified at the same time.

    autoIndexId

    boolean

    If this parameter is set to false, an index cannot be automatically created in the _id field.

    size

    number

    Optional. For a capped collection, this parameter specifies the maximum size of the collection.

    max

    number

    Optional. For a capped collection, this parameter specifies the maximum number of documents that can be stored in the collection.

    For details, see the official document.

    If the following information is displayed, the creation is successful:

    { "ok" : 1 }

  2. Inserts a record into the collection.

    db.coll.insert({"name": "sample"})

  3. View existing collections.

    show collections

  4. Delete the collection.

    db.coll.drop()

Creating a Capped Collection

Capped collections are fixed-size collections. Once a collection is full, it makes room for new documents by overwriting the oldest documents in the collection.

Run the following command to create a collection. The maximum size of the collection is 5 MB, and the maximum number of documents is 5,000.

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

Creating a Sharded Collection

In the DDS cluster architecture, you can create shards to fully utilize database performance. For details about the rules and suggestions for creating shards, see Sharding.

  1. Enable sharding on the database.

    sh.enableSharding("info")

  2. Create a sharded table and specify the shard key. The following command means that the fruit collection in database info is sharded using shard key _id.

    sh.shardCollection("info.fruit", {"_id": "hashed"})

    DDS sharded clusters support two types of sharding policy:

    • Range-based sharding allows querying a range of rows by the shard key values.
    • Hashed sharding evenly distributes writes to each shard.

Deleting a Collection

Run db.collection_name.drop() to delete the collection.