当前位置:网站首页>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 !!!️
边栏推荐
- 光的粒子说(光电效应/康普顿效应)
- 用Pycharm开发Flask框架设置debug模式没有效果的解决办法
- Arrangement of basic electrical knowledge (II)
- Introduction notes to machine learning
- 电学基础知识整理(二)
- 02 MongoDB数据类型、重要概念以及shell常用指令
- Meichuang data security management platform has obtained the evaluation certificate of "data security product capability verification plan" of the Institute
- English notes - cause and effect
- 05 MongoDB对列的各种操作总结
- Door level modeling - learning notes
猜你喜欢
Several ways of sharing printers in LAN
Chapter 1 Introduction to bash
数字电路学习笔记(二)
MSC 307(88) (2010 FTPC Code)第2部分烟气和毒性测试
工业物联网将取代人工发展吗?
几个重要的物理概念
Chapter 14 AC-DC power supply front stage circuit note I
【小程序实战系列】电商平台源码及功能实现
Arrangement of basic electrical knowledge (II)
A Preliminary Study of Blackbody radiation
随机推荐
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
双亲委派机制的理解学习
[graduation season] graduate summary
上线MES系统后,企业发生了这些变化......
English语法_形容词/副词3级 - 比较级
电学基础知识整理(一)
Unity C # e-learning (11) -- custom protocol generation tool
How to write anti shake throttling for small programs?
美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
几个重要的物理概念
基于正点原子stm32的mini板的TFTLCD显示实验
如何修改SE38编辑器主题
谈云原生,不得不谈的容器
Staggered and permutation combination formula
开关电源—Buck电路原理及其仿真
applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
How the uni app automatically switches the requested address according to the environment
@Transactional失效的几种场景
MSc 307 (88) (2010 FTPC code) Part 5 low flame spread test