当前位置:网站首页>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 !!!️
边栏推荐
- Unity C# 网络学习(十一)——自定义协议生成工具
- Cannot edit in read-only editor if it appears in vscode
- Market competitiveness of robot programming education
- 04 MongoDB各种查询操作 以及聚合操作总结
- ambari SSLError: Failed to connect. Please check openssl library versions.
- 光伏板怎么申请ASTM E108阻燃测试?
- Zipkin service link tracking
- Does the applet input box flash?
- MySQL 主从复制、分离解析
- 02 MongoDB数据类型、重要概念以及shell常用指令
猜你喜欢

A preliminary study of blackbody radiation

关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)

以自动化赋能转型,飞鹤乳业加速迈向数字化!

门级建模—学习笔记

KVM常用命令详解

加法器—笔记

How to learn a programming language systematically| Dark horse programmer

【小程序实战系列】电商平台源码及功能实现

MySQL 主从复制、分离解析

ambari SSLError: Failed to connect. Please check openssl library versions.
随机推荐
English语法_形容词/副词3级-比较级_常用短语
ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
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
gcd最大公约数
Does the applet image component not display pictures?
Chapter 14 AC-DC power supply front stage circuit note I
Notes to friendship chain
[graduation season] graduate summary
The operating mechanism of spectrogram in audio Science
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
Staggered and permutation combination formula
视频爆炸时代,谁在支撑视频生态网高速运行?
leetcode:494.数组中添加加减运算符得到指定值的所有方法
小程序输入框闪动?
第14章 AC-DC电源前级电路 笔记一
友链须知
Using elk to build a log analysis system (I) -- component introduction
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions