Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and delete collections in MongoDB

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to create and delete collections in MongoDB. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Create a collection

Grammatical format

Db.createCollection (name, {capped:, autoIndexId:, size:, max})

Parameter description

Name: the name of the collection to be created

Options: optional parameter that specifies options for memory size and indexing

Options parameter description

Parameter name Parameter Type Parameter description capped Boolean creates a fixed collection if it is true. The default is not to enable fixed collections, which refer to collections of fixed size, which automatically overwrite the oldest documents when the maximum value is reached. When the value is true, the size parameter must be specified. If autoIndexId Boolean is true, the index is automatically created in the _ id field. The default falsesize value specifies a maximum value for the fixed collection, which defaults to no limit. If capped is true, you also need to specify this field. The max value specifies the maximum number of documents contained in a fixed collection.

_ id:mongodb automatically generates _ id as the primary key when creating a document, but it is not self-increasing

When a fixed collection inserts a document, MongoDB first checks the size field of the fixed collection, and then checks the max field.

Examples of usage

Create a fixed collection myCollection with a total space size of 1024000 KB and a maximum of 10000 documents.

> use testswitched to db test > db.createCollection ("myCollection", {capped: true, autoIndexId: true, size: 1024000, max: 10000}) {"note": "the autoIndexId option is deprecated and will be removed in a future release", "ok": 1} > show collectionsmyCollection

"note": "the autoIndexId option is deprecated and will be removed in a future release". Officially does not approve of creating an index on _ id, which will be removed in future releases

In fact, in MongoDB, you don't need to create a collection. When you insert some documents, MongoDB automatically creates collections.

> show collectionsmyCollection > db.myCollection2.insert ({"name": "it was you", "age": 27}) WriteResult ({"nInserted": 1}) > show collectionsmyCollectionmyCollection2 >

Delete a collection

Grammatical format

Db.collectionName.drop ()

Replace collectionName with collection name

Return value

If the selected collection is successfully deleted, the drop () method returns true, otherwise it returns false.

Example

> show collectionsmyCollectionmyCollection2 > db.myCollection2.drop () true > show collectionsmyCollection Thank you for reading! This is the end of this article on "how to create and delete collections in MongoDB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 267

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report