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

The operation method of MongoDB collection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to operate the MongoDB set". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to operate the MongoDB set.

MongoDB collection Collection operation

A collection in MongoDB is a set of documents, equivalent to tables in a relational database

MongoDB uses the db.createCollection () function to create a collection.

Syntax format: db.createCollection (name, options)

Name: collection name

Option: optional parameter object that specifies options for memory size and indexing

Field

Types

Description

Capped

Boolean

(optional) if true, a fixed collection is created. A fixed set is a collection of fixed size that automatically overwrites the earliest documents when the maximum is reached.

When the value is true, the size parameter must be specified.

AutoIndexId

Boolean

(optional) if true, automatically create an index in the _ id field. The default is false.

It is out of date and will be deleted in future versions.

Size

Numerical value

(optional) specify a maximum value (in bytes) for the fixed collection.

If capped is true, you also need to specify this field.

Max

Numerical value

(optional) specifies the maximum number of documents contained in a fixed collection.

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

1. Create a collection by default

In MongoDB, we don't have to create a collection. When we insert some data, we automatically create a collection and use the collection name in the document management command as the name of the collection. Document management commands are explained in detail in subsequent courses.

If authentication is turned on, you need to create an access user for the new database.

Insert data into the database

> use db1switched to db db1 > db.auth ('u5 id id: ObjectId ("5f72a49b8b17a978aa695f66") 1 > db.db1.insert ({"nInserted": 1}) WriteResult ({"nInserted": 1}) > db.db1.find () {"_ id": ObjectId ("5f71f8b8cc3767d5919019bf"), "user": "U5", "pwd": "U5"} {"_ id": ObjectId ("5f72a49b8b17a978aa695f66"), "K1": "v1"} >

two。 Query set

Show collections | show tables

3. Create a collection without parameters

> db.createCollection ('dev') {"ok": 1} > show collectionsdb1dev > db.dev.find () > db.dev.insert ({"K1": "v1"}) WriteResult ({"nInserted": 1}) > db.dev.find () {"_ id": ObjectId ("5f72a5368b17a978aa695f67"), "K1": "v1"}

4. Create a collection with parameters

> db.createCollection ('dev2', {capped:true,autoIndexId:true,size:2000000,max:1000}) {"note": "the autoIndexId option is deprecated and will be removed in a future release", "ok": 1} > show collectionsdb1devdev2 > show tablesdb1devdev2

5. Delete a collection

Users with the role of dbAdminAnyDatabase are required to operate

If we want to delete the collection, we need to switch to the database where the collection needs to be deleted. Use the drop () function to delete the collection.

The syntax format for deleting a collection is: db. The collection name. Drop ().

Delete the test2 collection

> db.dev2.drop () true > show tablesdb1dev > at this point, I believe you have a deeper understanding of "how to operate the MongoDB collection". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report