当前位置:网站首页>05 mongodb summary of various column operations
05 mongodb summary of various column operations
2022-06-28 04:05:00 【cui_ yonghua】
Catalog : The basic chapter ( Can solve the problem of 80 The problem of )
01 MongoDB Overview 、 Application scenarios 、 Download mode 、 Connection mode and development history, etc
02 MongoDB data type 、 Key concepts and shell Commonly used instructions
03 MongoDB Various additions to documents 、 to update 、 Delete operation summary
04 MongoDB Various query operations And summary of aggregation operations
05 MongoDB Summarize the various operations of the column
python3 operation MongoDB Various cases of
One . Change column names
Case study 1: modify age by 31 Of the column address Change the name of the column to address2, Only one record will be modified .
db.person.update({
age:31},{
$rename:{
address:'address2'}})
Case study 2: name For Zhang San's address Change the name of the column to address2, All records that meet the conditions will be modified .
db.person.update({
name:' Zhang San '},{
$rename:{
address:'address2'}},{
multi:true})
Two . Adding or deleting Columns
Update specific fields :
db.game.update({
"_id": 123}, {
"$set": {
"count": 10000}})
# amount to sql Medium
update game set count = 10000 where _id = 123;
Delete specific fields :
db.game.update({
"_id":123}, {
"$unset": {
"author":1}})
Case study 1: Add column name as name The value of is the column of sheet three , Only one more .
db.person.update({
name:' Zhang San '},{
$set:{
age:''}})
Case study 2: Add a column to the set age, The default is empty. , This column will be added to all documents :
db.person.update({
name:' Zhang San '},{
$set:{
age:''}}, {
multi:true})
Case study 3: Delete column named name The value of is the column of sheet three , Column names and column values are deleted , Only one item will be deleted .
db.person.update({
name:' Zhang San '},{
$unset:{
age:''}})
Case study 4: Delete column named name The value of is the column of sheet three , Column names and column values are deleted , Those that meet the conditions will be deleted .
db.person.update({
name:' Zhang San '},{
$unset:{
age:''}},{
multi:true})
3、 ... and . Update and insert a column field
3.1 $inc: To increase or decrease
The corresponding field must be a number , And the increasing or decreasing value must also be a number .
Case study 1: Give a column self growth $inc ,_id by 1 The record of ,age increase 1
# Every time age all 10
db.person.update({
_id:1},{
$inc: {
age:10}})
Case study 2: change 1 Bar record
db.person.update({
name:' Zhang San '},{
$inc:{
age:10}})
Case study 3: Change all records that meet the conditions
db.person.update({
name:' Zhang San '},{
$inc:{
age:10}},{
multi:ture})
3.2 Array append
$push Array append ,
Case study 1: Add an element
db.game.update({
"_id": 123}, {
"$push": {
"score": 123}})
Case study 2: Add multiple elements
db.game.update({
"_id": 123}, {
"$push": {
"score": [12,123]}})
notes : The append field must be an array . If the array field does not exist , It will be added automatically , Then add .
3.3 Add more than one element at a time
$pushAll Add more than one element at a time
db.game.update({
"_id": 123}, {
"$pushAll": {
"score": [12,123]}})
3.4 Appending non repeating elements
$addToSet: Appending non repeating elements , Like a collection Set, Increase only if the value is not in the element :
db.game.update({
"_id": 123}, {
"$addToSet": {
"score": 123}})
3.5 Other field operations
Case study 1: without _id by 4 The record of , The insert
db.person.update({
_id:4},{
$set: {
name:' Li Si ',class:' Class three ',score:90}},{
upsert:true})
# Inserted data results
{
"_id" : 4, "name" : " Li Si ", "class" : " Class three ", "score" : 90 }
Case study 2: If the row is not updated , Insert additional columns :$setOnInsert,upsert:true
db.person.update({
_id:5},{
$setOnInsert:{
name:' Wang Wu ',like:'football'}},{
upsert:true})
# After execution , The following records are added to the database :
{
"_id" : 5, "like" : "football", "name" : " Wang Wu " }
# If there is _id by 5 The record of , Execute the following statement , There will be no impact , No new , It won't change .
db.person.update({
_id:5},{
$setOnInsert:{
name:' Wang Wu ',like:'football',height:178}},{
upsert:true});
Case study : If the row is not updated , Insert additional columns :$setOnInsert,upsert:true
db.person.update({
_id:5},{
$setOnInsert:{
name:' Wang Wu ',like:'football'}},{
upsert:true})
# If there is _id by 5 The record of , Execute the following statement , There will be no impact , It will not be added or modified
db.person.update({
_id:5},{
$setOnInsert:{
name:' Wang Wu ',like:'football',height:178}},{
upsert:true})
Four . Remove elements
4.1 Delete an element
$pop Remove elements , Only one element in the array can be deleted at a time ,1 Means delete the last ,-1 To delete the first .
# Delete the last element
db.game.update({
"_id": 123}, {
"$pop": {
"score": 1}})
# Delete first element
db.game.update({
"_id": 123}, {
"$pop": {
"score": -1}})
Case study : Delete specific elements
db.game.update({"_id": 123}, {"$pull": {"score": 123}})
4.2 Delete specific elements
$pull Delete specific elements
# Delete array score The internal value is equal to 123 The elements of .
db.game.update({
"_id": 123}, {
"$pull": {
"score": 123}})
4.3 Delete multiple specific elements
$pullAll Delete specific elements
# Delete the values in the array equal to 123 or 12 The elements of .
db.game.update({
"_id": 123}, {
"$pullAll": {
score: [123,12]}})
️ If it works , Thank you for clicking three times !!!️
边栏推荐
猜你喜欢

美创入选“2022 CCIA中国网络安全竞争力50强”榜单

Pycharm setting pseudo sublime color scheme

MySQL 主从复制、分离解析

美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书

一文告诉你什么是 Kubernetes

Several important physical concepts

Door level modeling - learning notes

黑體輻射初探

How to learn a programming language systematically| Dark horse programmer

How to write a software test report? Here comes the third party performance report template
随机推荐
English grammar_ Adjective / adverb Level 3 - Comparative
黑體輻射初探
MSc 307 (88) (2010 FTPC code) Part 9 bedding test
2021年终总结及2022年展望
几个重要的物理概念
组件拆分实战
友链须知
一文告诉你什么是 Kubernetes
A solution to the inefficiency of setting debug mode in developing flask framework with pychar
等保三级密码复杂度是多少?多久更换一次?
数字电路学习笔记(一)
如何系统学习一门编程语言? | 黑马程序员
@Transactional失效的几种场景
内卷、躺平与中年危机的相关思考
数字有为,易步到位 华为携“5极”明星产品加速布局商业市场
基于正点原子stm32的mini板的TFTLCD显示实验
Using elk to build a log analysis system (I) -- component introduction
leetcode:单调栈结构(进阶)
Principle and Simulation of switching power supply buck circuit
Web APIs DOM event foundation dark horse programmer