当前位置:网站首页>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)
边栏推荐
猜你喜欢
MQ----activeMq
AD637 usage notes
Uni app Bluetooth communication
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
DBeaver同时执行多条insert into报错处理
matlab绘制hsv色轮图
场景化面试:关于分布式锁的十问十答
EBS Oracle 11g 克隆步骤(单节点)
Efficiency difference between row first and column first traversal of mat data types in opencv
Zhang Lijun: penetrating uncertainty depends on four "invariants"
随机推荐
JMeter installation under win7
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
AD637 usage notes
GCC9.5离线安装
场景化面试:关于分布式锁的十问十答
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
Net small and medium-sized enterprise project development framework series (one)
Ethereum ETH的奖励机制
华为快游戏调用登录接口失败,返回错误码 -1
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
"Grain mall" -- Summary and induction
1.2 download and installation of the help software rstudio
R language learning notes
Interview questions for basic software testing
Robot framework setting variables
SQL knowledge leak detection
張麗俊:穿透不確定性要靠四個“不變”
初级软件测试必问面试题
Daily question brushing record (XIV)
Problems encountered in office--