tencent cloud

TencentDB for MongoDB

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Strengths
Use Cases
Cluster Architecture
Product Specifications
Features
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
MongoDB Pricing
Billing Formula
Payment Overdue
Backup Space Billing
Configuration Adjustment Billing
Getting Started
Quickly Creating an Instance
Connecting to a TencentDB for MongoDB Instance
Reading/Writing Database
Operation Guide
Access Management
Instance Management
Node Management
Version Upgrade
Network Configuration
Monitoring
Backup and Rollback
Database Audit
Data Security
SSL Authentication
Log Management
Database Management
Multi-AZ Deployment
Disaster Recovery/Read-Only Instances
Parameter Configuration
Recycle Bin
Task Management
Performance Optimization
Data Migration Guide
Practical Tutorial
Optimizing Indexes to Break Through Read/Write Performance Bottlenecks
Troubleshooting Mongos Load Imbalance in Sharded Cluster
Considerations for Using Shard Clusters
Sample of Reading and Writing Data in MongoDB Instance
Methods for Importing and Exporting Data Based on CVM Connected with MongoDB
What to Do for Errors of Repeated Instance Creation and Deletion of Databases with the Same Names?
Troubleshooting MongoDB Connection Failures
Shard Removal Task: Guide for Confirming the Progress and Troubleshooting Issues
Performance Fine-Tuning
Ops and Development Guide
Development Specifications
Command Support in Sharded Cluster v3.2
Command Support in v3.6
Development Ops
Troubleshooting
Increased Slow Queries
Number of Connections Exceeding Limit
API Documentation
History
Introduction
API Category
Making API Requests
Instance APIs
Backup APIs
Account APIs
Other APIs
Task APIs
Introduction
Data Types
Error Codes
Instance Connection
Shell Connection Sample
PHP Connection Sample
Node.js Connection Sample
Java Connection Sample
Python Connection Sample
Python Read/Write Sample
Go Connection Sample
PHP Reconnection Sample
Product Performance
Test Environment
Test Method
Test Result
FAQs
Cost
Features
Sharded Cluster
Instance
Rollback and Backup
Connection
Data Migration
Others
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Reading/Writing Database

PDF
フォーカスモード
フォントサイズ
最終更新日: 2024-01-15 14:34:08
After you connect to the database instance, you can create databases and write data to them.

Creating a Database

The syntax to create a MongoDB database is as follows:
use DATABASE_NAME
Create a database named "myFirstDB" and insert a document to it:
> use myFirstDB
switched to db myFirstDB
> db.myFirstDB.insert({"test":"myFirstDB"})
WriteResult({ "nInserted" : 1 })
Show the database you created:
> show dbs
admin 0.000GB
config 0.000GB
local 0.041GB
myFirstDB 0.000GB

Creating a Collection

In MongoDB, you can use the createCollection() method to create a collection. Syntax:
db.createCollection(name, options)
Parameter description:
name: the name of the collection to create
options: (optional) options of memory size and index
options Field
Type
Description
capped
BOOL
Whether to set a maximum size in bytes for the collection. Valid values: true (the size field must be specified), false (default)
autoIndexId
BOOL
Whether to automatically create an index on the \\_id field. Valid values: true, false (default)
size
number
The maximum size in bytes of the collection
max
number
The maximum number of documents in the collection
Create a collection named "FirstCol" in the myFirstDB database:
> use myFirstDB
switched to db myFirstDB
> db.createCollection("FirstCol")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1634821900, 2),
"signature" : {
"hash" : BinData(0,"WFu7yj8wjeUBWG3b+oT84Q8wIw8="),
"keyId" : NumberLong("6990600483068968961")
}
},
"operationTime" : Timestamp(1634821900, 2)
}
Show the collection you created:
> show collections
FirstCol
The following sample shows that the FirstCol collection you created can have up to 10,000 documents whose total size cannot exceed 6,142,800 bytes.
> db.createCollection("FirstCol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } )
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1634821879, 1),
"signature" : {
"hash" : BinData(0,"EuIbp2fu9Yh38HOBHLgYqljdKaE="),
"keyId" : NumberLong("6990600483068968961")
}
},
"operationTime" : Timestamp(1634821879, 1)
}

Inserting a Document

In MongoDB, you can use the insert() or save() method to insert a document to a collection, as shown below:
> db.FirstCol.insert({name:"Jane Smith",sex:"Female",age:25,status:"A"})
WriteResult({ "nInserted" : 1 })
Show the document inserted to the collection:
> db.FirstCol.find()
{ "_id" : ObjectId("61716957a6fe1ef4d7eae979"), "name" : "Jane Smith", "sex" : "Female", "age" : 25, "status" : "A" }
You can use db.collection.insertMany() to insert one or more documents to a collection, as shown below:
db.collection.insertMany(
[ <document 1> , <document 2>, ... ]
)
Sample:
> db.FirstCol.insertMany([{name:"Mary Smith",sex:"Female",age:25,status:"A"},{name:"John White",sex:"Male",age:26,status:"B"},{name:"Michael White",sex:"Male",age:26,status:"A",groups:["news","sports"]}])
{

"acknowledged" : true,
"insertedIds" : [
ObjectId("617282a3a4bb72d733b5c6d7"),
ObjectId("617282a3a4bb72d733b5c6d8"),
ObjectId("617282a3a4bb72d733b5c6d9")
]
}

Updating a Database

In MongoDB, you can use update() to update documents in a collection.
Update the data in the FirstCol collection where name is Mary Smith:
> db.FirstCol.update({name:"Mary Smith",sex:"Female",age:25,status:"A"},{$set:{'age':28}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Show the result:
> db.FirstCol.find().pretty()
{
"_id" : ObjectId("618904b6258a6c38daf13abd"),
"name" : "Mary Smith",
"sex" : "Female",
"age" : 28,
"status" : "A"
}
{
"_id" : ObjectId("618904b6258a6c38daf13abe"),
"name" : "John White",
"sex" : "Male",
"age" : 26,
"status" : "B"
}
{
"_id" : ObjectId("618904b6258a6c38daf13abf"),
"name" : "Michael White",
"sex" : "Male",
"age" : 26,
"status" : "A",
"groups" : [
"news",
"sports"
]
}

Deleting a Database

In MongoDB, you can use remove() to delete documents from a collection, as shown below:
> db.FirstCol.remove({name:"Mary Smith",sex:"Female",age:28,status:"A"})
WriteResult({ "nRemoved" : 1 })
Show the result:
> db.FirstCol.find().pretty()
{
"_id" : ObjectId("618904b6258a6c38daf13abe"),
"name" : "John White",
"sex" : "Male",
"age" : 26,
"status" : "B"
}
{
"_id" : ObjectId("618904b6258a6c38daf13abf"),
"name" : "Michael White",
"sex" : "Male",
"age" : 26,
"status" : "A",
"groups" : [
"news",
"sports"
]
}

References

For more information, see MongoDB official documentation.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック