当前位置:网站首页>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 .
边栏推荐
- [Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video
- 利用GrayLog告警功能实现钉钉群机器人定时工作提醒
- Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
- wyt 。。
- 16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
- 一文带你吃透js处理树状结构数据的增删改查
- List uses stream flow to add according to the number of certain attributes of the element
- Data communication foundation NAT network address translation
- obj集合转为实体集合
- 18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
猜你喜欢
单商户 V4.4,初心未变,实力依旧!
研发效能度量指标构成及效能度量方法论
后台系统发送验证码功能
Use of RLOCK lock
Six common transaction solutions, you sing, I come on stage (no best, only better)
SQL injection sqllabs (basic challenges) 1-10
ES6 drill down - ES6 generator function
Intel 13th generation Raptor Lake processor information exposure: more cores, larger cache
六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
我们为什么要学习数学建模?
随机推荐
Appium automation test foundation - appium basic operation API (II)
Quick completion guide for manipulator (IX): forward kinematics analysis
ES6 drill down - ES6 generator function
Analytic hierarchy process of mathematical modeling (including Matlab code)
DataArts Studio数据架构——数据标准介绍
sql中查询最近一条记录
obj解析为集合
vulnhub-Root_ this_ box
MySQL overview
Mistakes made when writing unit tests
obj集合转为实体集合
Data communication foundation ACL access control list
【毕业季】作为一名大二计科在校生,我有话想说
异常com.alibaba.fastjson.JSONException: not match : - =
践行自主可控3.0,真正开创中国人自己的开源事业
Convert obj set to entity set
The list set is summed up according to a certain attribute of the object, the maximum value, etc
单商户 V4.4,初心未变,实力依旧!
Appium automation test foundation - appium basic operation API (I)
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)