Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The operation method of MongoDB collection

Shulou Source: shulou.com Published: 2022-06-02 06:13:50 09月25日 Update

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!

Tags: Data document field method parameter name database maximum content function command size Boolean numeric maximum format user index syntax learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker vpn MySQL Huawei