当前位置:网站首页>How to add new fields to mongodb with code (all)
How to add new fields to mongodb with code (all)
2022-07-05 21:56:00 【Menon research monk】
Catalog
Preface
About MongoDB See my previous article for more information :
MongoDB Introduction to framework zero foundation
In itself MongoDB The connection is just like Mysql The same as your database
Mysql How to connect :mysql -u -root -p( In standard mode )
MongoDB similar :mongo -u root -p
The reason for adding field values
It's usually python web In the frame , stay form A field value is added to the form and written into the database ( Only the latest data has this field value )
Field values that have no previous data can only be added through the database
The specific adding method can be added by one click in the database or script ( Both methods are similar )
1. Add
Add field values in the database
adopt update Just add
adopt mongo Connected to the ( Distinguish version number 、 user name 、 password 、 Document collection 、 Port number, etc )
My connection method is :mongo Version number --host xxx -u user name -p " password " Document collection --port Port number
The link will show mongo Command entry ( similar mysql)
Add the required document collection field value in the database
At the same time, for the sake of conservatism , Through the first find Query whether there is this field
The format is as follows :db. Document collection .find({ Field value Dictionary }).pretty()
The code example is as follows :
db.manong.find({"age":18}).pretty()
After finding that the field value has a value update Update and add a field value
The format is as follows :db.collection.update({query},{$set:{"":""}})
db.manong.update({
"age":18},{
$set:{
'title':' Code the agriculture - Research monk '}})
Re pass find Query whether there is the field value
If all data is added, the value of this field is 'title':' Code the agriculture - Research monk '
The code is as follows :
db.manong.update({
},{
$set:{
'title':' Code the agriculture - Research monk '}})
2. Script add
It is conservative and safe to modify the script with one click
The main reason is that the terminal may make a fatal error by slightly typing the wrong command enter
The specific script only needs to be connected mongo、 Inquire about mongo Whether there is this data value (debug)、 Add new field values accordingly
( Add this field value to all data ) The complete code is as follows :
import datetime
from django.core.management.base import BaseCommand
# Bring in the connection mongo Function of , Write on the outside , Don't put connection functions in your code
from xx.common import get_mongo
class Command(BaseCommand):
def handle(self, *args, **options):
# Connect mongo
mongo = get_mongo()
values = {
"$set": {
"title":" Code the agriculture - Research monk "
}
}
mongo[" aggregate "].update({
}, values, multi=True)
# debug Query display value
for record in mongo[" aggregate "].find():
logger.info(record)
adopt python xx.py( file name ) Execute the script
If the virtual environment is set, execute the script
See my article for details :
Windows Configure virtual environment and common commands ( Graphic analysis )
The other is :
The script of adding this field value to a certain data is as follows :( Core code )
params = {
"age": 18
}
values = {
"$set": {
"title":" Code the agriculture - Research monk "
}
}
mongo[" aggregate "].update(params, values)
# debug Query display value
for record in mongo[" aggregate "].find():
logger.info(record)
边栏推荐
- oracle 控制文件的多路复用
- ESP32
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- Analysis and test of ModbusRTU communication protocol
- Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
- Huawei cloud modelarts text classification - takeout comments
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- Shell script, awk uses if, for process control
- POJ 3237 tree (tree chain splitting)
- one hundred and twenty-three thousand four hundred and fifty-six
猜你喜欢

MMAP learning

Dbeaver executes multiple insert into error processing at the same time

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

Efficiency difference between row first and column first traversal of mat data types in opencv

uni-app 蓝牙通信

matlab绘制hsv色轮图

总结出现2xx、3xx、4xx、5xx状态码的原因

Drawing HSV color wheel with MATLAB

Xlrd common operations

Oracle checkpoint queue - Analysis of the principle of instance crash recovery
随机推荐
怎么利用Tensorflow2进行猫狗分类识别
初级软件测试必问面试题
POJ 3237 tree (tree chain splitting)
1.2 download and installation of the help software rstudio
poj 3237 Tree(树链拆分)
Poj3414 extensive search
Drawing HSV color wheel with MATLAB
GCC9.5离线安装
Ethereum ETH的奖励机制
Poj 3237 Tree (Tree Chain Split)
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
張麗俊:穿透不確定性要靠四個“不變”
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
华为快游戏调用登录接口失败,返回错误码 -1
Incentive mechanism of Ethereum eth
The American Championship is about to start. Are you ready?
Codeforces 12D ball tree array simulation 3 sorting elements