Document Database Service

How to Create and Manage Collection

2025-07-28 08:06:23

Create Collection

In DDS, the steps to create a collection are as follows:

1.First use or create a database.

use mydatabase

2.Create a collection using the db. createCollection() method.

db.createCollection("authors")

3.Collections can also be created dynamically when inserting documents.

db.authors.insertOne({name: "John"})

In the above statement, the authors collection is automatically created when the document is inserted.

4.Specify optional parameters to create a collection.

db.createCollection(<name>, { capped: <boolean>,//Whether to create a fixed-size collection, default to false
                              autoIndexId: <boolean>,//If it is specified as false, the automatic index creation in _id field is disabled
                              size: <number>,//If you are creating a fixed-size collection, specify the size of the collection in bytes, the default is 4096 bytes
                              max: <number>,//If you are creating a fixed-size collection, specify the maximum number of documents in the collection, which is unlimited by default
                              storageEngine: <document>,//Specifies the storage engine for the collection, such as WiredTiger or MMAPv1
                              validator: <document>,//Specifies the validator of the collection, which is used to verify whether the inserted or modified document conforms to the specified rules
                              validationLevel: <string>,//validationLevel`This option is used to specify the validation level for the collection document, with the default value off (turn off document validation) and the optional values moderate (basic document structure validation) and strict (strict mode validation)
                              validationAction: <string>,//Specifies the behavior when document validation fails. The default value is error (when document validation fails, no write is allowed). And a warn (warns when document validation fails, but still allows writing)
                              indexOptionDefaults: <document>,//Specifies default options for indexes in the collection, such as uniqueness, sparsity, and expiration time
                              viewOn: <string>,//If you are creating a view collection, specify the source collection for the view
                              pipeline: <pipeline>,//If you are creating a view collection, specify the view's aggregation pipeline for aggregation operations on the source collection
                              collation: <document>,//Used to specify the collation rules for the collection
                              writeConcern: <document>//Used to specify the write concern level for the collection operation
                              } 
)

The echo message is as follows, indicating that the creation is successful: { "ok" : 1 }

5.View all collections in the current database.

show collections

6.Delete the collection using db. collection.drop()

db.authors.drop()

7.Rename the collection using db.renameCollection().

db.renameCollection("oldCollection", "newCollection")

Please note when creating a collection:

  • The collection name cannot start with "system", ".", or "$".

  • The collection name should not exceed 128 bytes.

  • The collection name cannot contain ".".

Create Fixed Collection

Fixed collections refer to those collections that have a maximum size or number of documents. When the size or number of the collection exceeds the maximum value, the oldest stored value in the collection will be automatically deleted.

The following command creates a collection with a maximum size of 50MB and a maximum number of documents of 500,000.

db.createCollection("bookmark", { capped : true, size : 52428800, max : 500000 } )

Create Shard Collection

In the DDS sharded cluster, to use the sharding function, you must first create a shard collection.

The main steps are:

1.First, create a sharding key index. The shard collection must have a sharding key index.

db.collection.createIndex({ <shard key> : 1 }, {name: <index name>})

For example:

db.users.createIndex({ "user_id": 1 }, {name: "userid_1"})//Here 1 indicates that the ascending index is specified

2.Use sh.enableSharding() to enable sharding.

sh.enableSharding("<database name>")

3.Then use sh.shardCollection() to shard the collection.

sh.shardCollection("<database name>.<collection name>", 
                  { <shard key> : 1 },
                  { <options> }  
)

For example:

sh.shardCollection("mydatabase.users", 
                  { "user_id" : 1 },    
                  { "requireIndex" : true }
)

Here we use user_id as the sharding key.

4.If you want to split an existing collection, you can use the splitAt option in shardCollection.

sh.shardCollection("mydatabase.largecollection",  
                   { score : 1 },  
                   { splitAt : { score : 1000 } }
)

Here we specify that the collection is split at score = 1000, which means that documents with a value of "score" field less than or equal to 1,000 are assigned to one shard, and documents with a value of "score" field greater than 1,000 are assigned to another.

By following the above steps, you can create a shard collection. DDS will automatically distribute the collection data to various shards.

DDS sharded cluster supports two sharding policies:

  • Range sharding, which supports range query based on Shard Key.

  • Hash sharding, which evenly distributes writes to each Shard.

Delete Collection

Execute db.collection_name.drop() to delete the entire collection.


lGH6M7DOb5i7