当前位置:网站首页>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)
边栏推荐
- 华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
- Ethereum ETH的奖励机制
- Oracle检查点队列–实例崩溃恢复原理剖析
- MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
- 极狐公司官方澄清声明
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- How to organize an actual attack and defense drill
- Incentive mechanism of Ethereum eth
- HDU 4391 paint the wall segment tree (water
- Shell script, awk condition judgment and logic comparison &||
猜你喜欢
QML reported an error expected token ";", expected a qualified name ID
張麗俊:穿透不確定性要靠四個“不變”
Installation of VMware Workstation
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
How to use tensorflow2 for cat and dog classification and recognition
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Drawing HSV color wheel with MATLAB
PIP install beatifulsoup4 installation failed
随机推荐
[Yugong series] go teaching course in July 2022 004 go code Notes
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
NET中小型企业项目开发框架系列(一个)
Oracle HugePages没有被使用导致服务器很卡的解决方法
SQL knowledge leak detection
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
QML reported an error expected token ";", expected a qualified name ID
Robot framework setting variables
DBeaver同时执行多条insert into报错处理
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
Alibaba cloud award winning experience: build a highly available system with polardb-x
uni-app 蓝牙通信
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
The real situation of programmers
ESP32
华为联机对战如何提升玩家匹配成功几率
Cross end solutions to improve development efficiency
MMAP学习
854. 相似度为 K 的字符串 BFS