当前位置:网站首页>Mongodb getting started Tutorial Part 04 mongodb client
Mongodb getting started Tutorial Part 04 mongodb client
2022-07-05 16:10:00 【Teacher Tony who doesn't cut his hair】
This article introduces MongoDB Client tools mongo Use .
mongo shell
mongo shell Is a connection MongoDB The interactive JavaScript Interface .mongo shell Can be used to operate MongoDB Data in , You can also perform some management tasks .
mongo shell Be similar to MySQL Database client mysql,PostgreSQL client psql, perhaps Oracle In the database SQL*Plus Tools .
Be careful ,MongoDB v5.0 It is abandoned by default at the beginning MongoDB Installed together mongo shell, Recommend using the new mongsh.
In the use of mongo shell Before , We need to Download and install This tool .
After installation , Enter the following command on the command line :
mongosh
mongo shell Will automatically connect to the local (localhosr) Default port (27017) Upper MongoDB service .
mongo shell That is, it can be used as a fully functional JavaScript Interpreter , Can also be used as a MongoDB Client tools .
JavaScript Interpreter
mongo shell It's a fully functional one JavaScript Interpreter , So we can use it to execute JavaScript Code . for example :
> Math.max(10,20,30);
30
mongo shell Allow to enter cross line commands , When we enter enter, it will detect JavaScript Whether the statement is complete . If the statement is incomplete , We can continue to enter on the next line :
> function add(a, b) {
... return a + b;
... }
> add(10,20);
30
Input console.clear() The command can clear the screen :
console.clear()
MongoDB client
mongo shell It's a MongoDB client . By default , It will connect to the local MongoDB In service test database , And set the database connection as a global variable db.
db Variables can be used to view the current database :
> db
test
except JavaScript Beyond grammar ,mongosh It also provides a lot of convenience for us to communicate with MongoDB Commands exchanged by the database server . for example ,shows dbs The command can list all databases in the service :
test> show dbs
admin 41 kB
config 73.7 kB
local 81.9 kB
The above output shows 3 A database .
If you want to switch the current database , have access to use command . for example , The following command can switch the current database to bookdb database :
test> use bookdb
switched to db bookdb
Be careful , We can switch to a nonexistent database . here , When we first saved data ,MongoDB This database will be created automatically .
After executing the above order , Variable db The value of is bookdb:
> db
bookdb
here , We can use variables db Access database bookdb The set in books:
> db.books
bookdb.books
Basic CRUD operation
Let's demonstrate how to create (Create)、 Read (Read)、 to update (Update) And delete (Delete) file . These operations are also known as CRUD.
This article only covers simple CRUD operation , We will learn more about CRUD operation .
create documents
If you want to create a new document in the collection , have access to insertOne() Method .
The following command is set books Added a new document ( A new book ):
db.books.insertOne({
title: "SQL Programming idea ",
published_year: 2021
})
The output is as follows :
{
"acknowledged" : true,
"insertedId" : ObjectId("62bb0962874071c64b1f9b7b")
}
When we enter enter ,mongo shell Will send the command to MongoDB The server . If the command is valid ,MongoDB Will insert the document and return the result .
In the example above , The return object contains two keys :acknowledged and insertedId.insertedId The value of is the document _id Field .
If we add documents without specifying _id Field ,MongoDB A unique... Is automatically assigned to the document ObjectId As _id Value of field .
MongoDB Use _id Fields uniquely identify documents in the collection .
Search for documents
If you want to find documents in the collection , have access to findOne() Method . for example :
db.books.findOne()
The output is as follows ;
{
_id: ObjectId("62bb0962874071c64b1f9b7b"),
title: 'SQL Programming idea ',
published_year: 2021
}
pretty() Method can format and display the output results , for example :
db.books.find().pretty()
{
"_id" : ObjectId("62bb0962874071c64b1f9b7b"),
"title" : "SQL Programming idea ",
"published_year" : 2021
}
Update the document
If you want to update the content of a document , have access to updateOne() Method . This method requires at least two parameters :
- The first parameter specifies the document to be updated .
- The second parameter specifies the content of the update operation .
The following example updates the title “SQL Programming idea ” The document published_year Field :
db.books.updateOne(
{
title: "SQL Programming idea "},
{
$set: {
published_year: 2022 }}
)
among , The first parameter indicates that the update title is “SQL Programming idea ” The first document of . The second parameter uses $set Operators update fields published_year Value . The results are as follows :
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Delete the document
If you want to delete a document in the collection , have access to deleteOne() Method . The method contains a parameter , Used to specify the document to delete .
The following example uses deleteOne() Method to delete a collection books The title is “SQL Programming idea ” The first document of :
db.books.deleteOne({
title: "SQL Programming idea "});
The output is as follows :
{
"acknowledged": true,
"deletedCount": 1
}
Returns... In the result deletedCount Indicates that a document has been successfully deleted .
If you want to view all collections in the current database , have access to show collections command :
show collections
books
database bookdb Currently, there is only one set in .
边栏推荐
- Data communication foundation ACL access control list
- Solve the Hanoi Tower problem [modified version]
- 践行自主可控3.0,真正开创中国人自己的开源事业
- Query the latest record in SQL
- Codasip adds verify safe startup function to risc-v processor series
- I'm fat, huh
- Data communication foundation - dynamic routing protocol rip
- How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
- vlunhub- BoredHackerBlog Moriarty Corp
- vlunhub- BoredHackerBlog Social Network
猜你喜欢

Research and practice of super-resolution technology in the field of real-time audio and video

Cs231n notes (medium) -- applicable to 0 Foundation
![[brief notes] solve the problem of IDE golang code red and error reporting](/img/b6/0b2ea06eb5fbe651ff9247b109fa15.png)
[brief notes] solve the problem of IDE golang code red and error reporting

OceanBase社区版之OBD方式部署方式本地安装
![[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video](/img/69/3aedcdafb2b4e83087dc1ce593dc38.png)
[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video

Use of set tag in SQL

基于OpenHarmony的智能金属探测器

vulnhub-Root_ this_ box
![19.[STM32]HC_ SR04 ultrasonic ranging_ Timer mode (OLED display)](/img/fe/8f59db28823290da8e9280df06673d.jpg)
19.[STM32]HC_ SR04 ultrasonic ranging_ Timer mode (OLED display)

Clock switching with multiple relationship
随机推荐
sql中set标签的使用
20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
漫画:什么是分布式事务?
The list set is summed up according to a certain attribute of the object, the maximum value, etc
APICloud云调试解决方案
数据湖(十四):Spark与Iceberg整合查询操作
Which keywords will conflict with the abstract keyword
Memo 00
Relationship between objects and classes
Clock switching with multiple relationship
Six common transaction solutions, you sing, I come on stage (no best, only better)
漫画:什么是MapReduce?
verilog实现计算最大公约数和最小公倍数
Data communication foundation - dynamic routing protocol rip
ES6 drill down - Async functions and symbol types
Obj resolves to a set
超分辨率技术在实时音视频领域的研究与实践
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
CISP-PTE之PHP伪协议总结
Data communication foundation OSPF Foundation