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

What is the MongoDB library and collection management like

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail about MongoDB library and collection management, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

* MongoDB library management

Db.version () / / View version

Use userdb / / switch if the library exists, and create it if it does not exist

Show dbs / / check the library, userdb does not appear at this time, this is because the library is empty, there are no collections, you just need to create a collection to see

Db.createCollection ('clo1') / / create a collection clo1, created under the current library

Db.dropDatabase () / / Delete the current library. To delete a library, you must switch to that library.

Db.stats () / / View the information of the current library

Db.serverStatus () / / View the status of the mongodb server

* MongoDB creates collections

Db.createCollection ("mycol", {capped: true, autoIndexID: true, size: 6142800, max: 10000}) / / Syntax: db.createCollection (name,options)

Name is the name of the collection. Options is optional and is used to configure the parameters of the collection. Parameters are as follows

Capped true/false (optional) enables capped collections if true. A capped collection is a collection of fixed size, and when it reaches its maximum size, it automatically overwrites the earliest entries. If you specify true, you also need to specify size parameters.

AutoindexID true/false (optional) if true, the default value for the automatically created index _ id field is false.

Size (optional) specifies the maximum size byte capped collection. If the cap is true, then you also need to specify this field. Unit B

Max (optional) specifies the maximum number of files allowed in the capped collection.

* MongoDB data management

Show collections / / View the collection, or use show tables

Db.Account.insert ({AccountID:1,UserName: "123", password:" 123456 "}) / / if the collection does not exist and the data is inserted directly, mongodb automatically creates the collection. Account is the name of the collection. This is followed by data.

Db.Account.update ({AccountID:1}, {"$set": {"Age": 20}}) / / updates are added, while existing domains are overwritten.

Db.Account.find () / / View all documents

Db.Account.find ({AccountID:1}) / / query based on criteria

Db.Account.remove ({AccountID:1}) / / delete according to condition

Db.Account.drop () / / Delete all documents, that is, delete the collection

Use dbname / / enter the corresponding library first

Db.printCollectionStats () / / View the status information of all collections in the library

Note: when creating a collection, you cannot start with a number, otherwise an error will be reported when inserting data.

About how the MongoDB library and collection management is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it 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: 0

*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