当前位置:网站首页>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)
边栏推荐
- 华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
- About the writing method of SQL field "this includes" and "included in" strings
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- sql常用语法记录
- poj 3237 Tree(樹鏈拆分)
- Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
- MQ----activeMq
- Yolov5 training custom data set (pycharm ultra detailed version)
- The primary key is set after the table is created, but auto increment is not set
- 华为云ModelArts文本分类–外卖评论
猜你喜欢
張麗俊:穿透不確定性要靠四個“不變”
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
MMAP learning
总结出现2xx、3xx、4xx、5xx状态码的原因
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Deeply convinced plan X - network protocol basic DNS
2.2.5 basic sentences of R language drawing
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
How can Huawei online match improve the success rate of player matching
随机推荐
ICMP 介绍
How to organize an actual attack and defense drill
PIP install beatifulsoup4 installation failed
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Daily question brushing record (XIV)
CRM creates its own custom report based on fetch
Summary of data analysis steps
kingbaseES V8R3数据安全案例之---审计记录清除案例
Oracle HugePages没有被使用导致服务器很卡的解决方法
Ethereum ETH的奖励机制
SQL common syntax records
The real situation of programmers
极狐公司官方澄清声明
Making global exception handling classes with aspect
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
ICMP introduction
AD637 usage notes
uni-app 蓝牙通信
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Create a virtual machine on VMware (system not installed)