当前位置:网站首页>Mongodb common commands
Mongodb common commands
2022-07-05 02:52:00 【qq_ fifty-two million two hundred and seventeen thousand two hu】
Catalog
3. View all tables under the current database directory
9. Query a piece of data in the table
10. Query all data in the table
11. Query the specified quantity data in the specified location table
12. Query table Condition display data
13. Condition query table data
14. Fuzzy / Regular query table data
15. Operator queries table data
16. Query the table data and sort and display
17. modify / Delete the attribute data specified in the table
(1). $set Modify the specified properties
(2). $unset Delete the specified attribute
(3). $inc Auto increment specified attribute
(4). $set $inc $unset Use a combination of Auto add, delete and modify
(5). $push Add members to the specified attribute array ,$each Add multiple members at once
(6). $pop Pop up a member at the end of the specified attribute array
(6). $pull Delete the specified member from the specified attribute array
1. View version number
db.version()2. View all databases
show dbs3. View all tables under the current database directory
show collections4. Select database
(1). So here's what I chose UserName database
(2). No error will be reported if the data does not exist , It will be recorded in memory , If you use add data operation , This database will be created
(3). If before executing other orders , You did not add data , Then the memory will be released
use UserName5. Create table
| createCollection Parameters | The default value is |
| Table name | nothing |
db.createCollection("students")6. Delete table
db.students.drop()7. insert data
| insert Parameters | The default value is |
| JOSN Array or single JSON | nothing |
If this table is not saved, it will be automatically created
db.students.insert({name:" Zhang San ",age:18,gender:" male "})db.students.insert([{name:" Wang Wu ",age:15,gender:" male "},{name:" Li Si ",age:19,gender:" male "}])8. Delete table data
| Delete a piece of data and return the object | findOneAndDelete Parameters | The default value is |
| 1. Delete the condition | nothing |
| Only one piece of data can be deleted | deleteOne Parameters | The default value is |
| 1. Delete the condition | nothing |
| Delete multiple data | deleteMany Parameters | The default value is |
| 1. Delete the condition | nothing |
| remove Parameters | The default value is |
| 1. Delete the condition | nothing |
| 2. Whether to delete only one | false |
by {} It means to clear all data in the table
db.students.remove({})Delete all in the table name For Zhang San's data
db.students.remove({"name":" Zhang San "})Delete the specified id data
db.students.remove({_id:ObjectId("6208af4dd5f0203f271bb2fa")})9. Query a piece of data in the table
| findOne Parameters | The default value is |
| Query criteria | nothing |
db.students.findOne()10. Query all data in the table
| find Parameters | The default value is |
| Query criteria | nothing |
db.students.find()11. Query the specified quantity data in the specified location table
(1). db.students.find().skip(2)
(2). dskip(2) Put the starting position inside , That is, query from the third data
(3).limit(5) How many pieces of data are extracted , Here are five pieces of data
db.students.find().skip(2)db.students.find().skip(2).limit(5)12. Query table Condition display data
(1). db. Table name .find()
(2). The first query condition {}, Empty means to display all data
(3). The second display condition {age:1},0 No display ,1 Show , Comma separated conditions
(4). This order is , All data is only displayed age data , If the display condition is 0 Will only be displayed except age All data except data
db.students.find({},{age:1})13. Condition query table data
(1). db. Table name .find()
(2). The first query condition {}, Empty means to display all data , Comma separated
(3). Inquire about name For all the data of Zhang San
(4). Inquire about name For Zhang San ,age by 18 Full data
db.students.find({name:" Zhang San "}) db.students.find({name:" Zhang San ",age:18})14. Fuzzy / Regular query table data
(1). db. Table name .find() perhaps db. Table name .findOne() This can only be queried
(2). Inquire about name There are pieces of data in it ,/ Regular expressions /
db.students.find({name:/ Zhang /})db.students.find({name:/^[a-z]{4}$/})15. Operator queries table data
(1). db. Table name .find() perhaps db. Table name .findOne() This can only be queried
(2). The first query condition {}, Empty means to display all data , Comma separated
(3). The conditions say Json,Json Write operators in ,($and and $or) exception , Here is a case
| Operator | |
| also | $and |
| perhaps | $or |
| Greater than | $gt |
| Less than | $lt |
| It's not equal to | $ne |
| Greater than or equal to | $gte |
| Less than or equal to | $lte |
| Include conditions | $in |
| No conditions are included | $nin |
Inquire about age be equal to 15 also gender Equal to a man , The following two implementation methods are the same
db.students.find({$and:[{age:15,gender:" male "}]})db.students.find({$and:[{age:15},{gender:" male "}]})Inquire about age be equal to 15 perhaps gender Equal to a man , Multiple Json
db.students.find({$or:[{age:15},{gender:" male "}]})The query is greater than 20 Of
db.students.find({age:{$gt:20}})Query is greater than or equal to 20 Of
db.students.find({age:{$gte:20}})Query less than... Etc 20 Of
db.students.find({age:{$lt:20}})Query is less than or equal to 20 Of
db.students.find({age:{$lte:20}})Inquiry is not equal to 20 Of
db.students.find({age:{$ne:20}})The query contains 20,19,15 Of
db.students.find({age:{$in:[20,19,15]}})The query does not contain 20,19,15 Of
db.students.find({age:{$in:[20,19,15]}})16. Query the table data and sort and display
| sort Parameters | value | |
| Sort parameters | Ascending | 1 |
| Descending | -1 | |
Fuzzy search for data with Zhang characters to sort in ascending order
db.students.find({name:/ Zhang /}).sort({age:1})Fuzzy search of data with Zhang characters for multiple conditional sorting , Multiple sort !
db.students.find({name:/ Zhang /}).sort({age:1,height:-1})17. modify / Delete the attribute data specified in the table
| update Parameters | Parameter format | The default value is |
| 1. filter | Json Format | nothing |
| 2. Modified properties | Json Format | nothing |
| 3. Whether to create | Boolean value | false |
| 4. Whether to modify in batch | Boolean value | false |
| The operator | effect |
| $set | modify |
| $unset | Delete |
| $inc | Self increasing |
| $push | Add members to the array |
| $each | coordination $push Add multiple members |
| $pop | Pop up tail members |
| $pull | Delete the specified member |
(1). $set Modify the specified properties
Here is the handle. name For Zhang San's age Attribute to 18, Other properties will not be deleted
db.students.update({name:" Zhang San "},{$set:{age:20}})Here is to put all less than or equal to 18 Of age Attribute to 18, Other properties will not be deleted
db.students.update({age:{$lte:18}},{$set:{age:19}},false,true)(2). $unset Delete the specified attribute
Here is the handle. name For Zhang San's age Attribute removed , And do not delete other attributes
db.students.update({name:" Zhang San "},{$unset:{age:19}})Note the addition operator $unset, Otherwise, his attribute will be cleared
Here is the handle. name For Zhang San's age Attribute to 18, And all other attributes will be deleted
db.students.update({name:" Zhang San "},{age:20})(3). $inc Auto increment specified attribute
The operator $inc send age Self increase on the original basis 2
db.students.update({name:" Zhang San "},{$inc:{age:2}})(4). $set $inc $unset Use a combination of Auto add, delete and modify
$set $inc $unset Use a combination of ,age Self increasing 2,gender Change to female , hold name Property deleted
db.students.update({name:" Zhang San "},{$inc:{age:2},$set:{gender:" Woman "},$unset:{name:""}})(5). $push Add members to the specified attribute array ,$each Add multiple members at once
The operator $push likes Array added new members
db.students.update({name:" Zhang San "},{$push:{likes:" sleep "}})add $each, You can add multiple members at a time
db.students.update({name:" Zhao Liuliu "},{$push:{likes:{$each:[" sleep "," To play basketball "," running "]}}})(6). $pop Pop up a member at the end of the specified attribute array
db.students.update({name:" Zhang San "},{$pop:{likes:1}})(6). $pull Delete the specified member from the specified attribute array
db.students.update({name:" Zhang San "},{$pull:{likes:" sleep "}})18. polymerization
| aggregate The operator | effect |
| $group | Perform distributed query ,( Such as the statistical average , Make a peace, etc ) |
| $match | By specifying attributes matching / Filter |
| $unwind | split array , Form each array into a new JSON data |
| $project | (1 Display only /0 Not shown ) Some attribute , Modify display name |
| _id | Grouping properties , It can be for null |
| $sum | similar Js in reduce() Cumulative sum of functions |
| $max | For maximum |
| $avg | averaging |
| $push | Add to array |
| $concat | Multi attribute concatenation string |
| Operator | |
| also | $and |
| perhaps | $or |
| Greater than | $gt |
| Less than | $lt |
| It's not equal to | $ne |
| Greater than or equal to | $gte |
| Less than or equal to | $lte |
| Include conditions | $in |
| No conditions are included | $nin |
(1). $sum Application
This is equivalent to that I put score Add values of attributes , Get the total
db.students.aggregate([
{
$group: {
_id: null,
count: {
$sum: "$score"
}
}}
])use cname Attribute to group , And calculate the score Total attribute value
db.students.aggregate([
{
$group: {
_id: "$cname",
count: {
$sum: "$score"
}
}}
])
group use cname Attribute and
math To match cname Attribute is equal to the ( In grade one ), And calculate the score Total attribute value
db.students.aggregate([
{$match: {cname:" In grade one "}},
{
$group: {
_id: "$cname",
count: {
$sum: "$score"
}
}}
])If match Later, you need to change the condition to _id

group use cname Attribute grouping and calculate the score Total attribute value , use math To filter lower than or equal to 300 The data of
Because the cumulative results are given above count all ,match I also need to write count Used for filtration
db.students.aggregate([
{$group: {
_id: "$cname",
count: {
$sum: "$score"
}
}},{$match: {count:{$gte:300}}},
])(2). $max Application
seek score The highest value in the attribute
db.students.aggregate([
{$group: {
_id:null,
count: {
$max: "$score"
}
}}
])First use cname Attribute to group , In use max Let's ask each group score The highest value of the attribute
db.students.aggregate([
{$group: {
_id: "$cname",
count: {
$max: "$score"
}
}}
])(3). $avg Application
First use cname Attribute to group , In use avg Let's ask each group score The average value of an attribute
db.students.aggregate([
{$group: {
_id:"$cname",
count: {
$avg: "$score"
}
}}
])(4). First use cname Attribute to group , In use push hold score Add the value of the property to the array
db.students.aggregate([
{$group: {
_id:"$cname",
count: {
$push: "$score"
}
}}
])(3). $unwind Application
First insert an array of data into the table
db.students.insert({name:" Zhang San ",age:19,gender:" Woman ",cname:" Third grade ",interset:[" having dinner "," sleep "," running "]})First use match Filter , In use $unwind by $interset Split into new data
db.students.aggregate([
{
$match: {
name:" Zhang San "
}
},
{
$unwind: "$interset"
}
])
(4). $project Application
hold _id Property does not display ,1 To display only this attribute ,0 To not display this attribute
db.students.aggregate([
{
$match: {
name:" Zhang San "
}
},
{
$unwind: "$interset"
},{
$project: {
_id:0
}
}
])Here is only id and name, The difference is that I put name The display is changed to username
db.students.aggregate([
{
$match: {
name:" Zhang San "
}
},
{
$unwind: "$interset"
},{
$project: {
_id:1,
username:"$name"
}
}
])(5). $concat Application
$concat Concatenate the attributes and strings in the array

db.students.aggregate([
{
$match: {
name:" Zhang San "
}
},
{
$unwind: "$interset"
},{
$project: {
_id:1,
info:{$concat:["$name","----","$cname"]}
}
}
])边栏推荐
- 有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- 【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
- Start the remedial work. Print the contents of the array using the pointer
- Marubeni Baidu applet detailed configuration tutorial, approved.
- The database and recharge are gone
- Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
- LeetCode --- 1071. Great common divisor of strings problem solving Report
- d3js小记
- Sqoop installation
猜你喜欢

openresty ngx_ Lua execution phase

【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)

openresty ngx_lua執行階段

This + closure + scope interview question

【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)

ELFK部署

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Sqoop installation

Exploration of short text analysis in the field of medical and health (I)
随机推荐
Azkaban实战
Openresty ngx Lua Execution stage
Sqoop installation
2021 Li Hongyi machine learning (2): pytorch
Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
问下,这个ADB mysql支持sqlserver吗?
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Azkaban overview
Six stone programming: advantages of automated testing
Kotlin - 协程 Coroutine
Yyds dry goods inventory intelligent fan based on CC2530 design
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
From task Run get return value - getting return value from task Run
2021 Li Hongyi machine learning (1): basic concepts
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
el-select,el-option下拉选择框
【微服务|SCG】Filters的33种用法
Simple use of devtools
The phenomenology of crypto world: Pioneer entropy
Design and implementation of community hospital information system