当前位置:网站首页>如何向mongoDB中添加新的字段附代码(全)
如何向mongoDB中添加新的字段附代码(全)
2022-07-05 21:52:00 【码农研究僧】
前言
关于MongoDB更多的知识点可看我之前这篇文章:
MongoDB框架零基础入门
本身MongoDB的连接就和Mysql的数据库一样
Mysql连接方式:mysql -u -root -p(标准模式下)
MongoDB类似:mongo -u root -p
之所以要增加字段值
一般都是python web框架中,在form表单内增加了一个字段值写入数据库(只有最新的数据才有这个字段值)
之前数据没有的字段值只能通过数据库添加
具体添加方式可以通过数据库内或者脚本一键添加(两种方式都差不多)
1. 数据库内增加
在数据库内增加字段值
通过update增加即可
通过mongo连接上(区分版本号、用户名、密码、文档集合、端口号等)
我的连接方式为:mongo版本号 --host xxx -u 用户名 -p "密码" 文档集合 --port 端口号
链接上去之后就会显示mongo命令入口(类似mysql)
在该数据库增加所要的文档集合字段值
增加的同时为了保守起见,先通过find查询是否有该字段
格式如下:db.文档集合.find({字段值字典}).pretty()
代码示例如下:
db.manong.find({"age":18}).pretty()
查询到该字段值有值之后对其update更新添加某个字段值
格式如下:db.collection.update({query},{$set:{"":""}})
db.manong.update({
"age":18},{
$set:{
'title':'码农- 研究僧'}})
再通过find进行查询是否有该字段值
如果是所有的数据都增加这个字段值为'title':'码农- 研究僧'
则代码如下:
db.manong.update({
},{
$set:{
'title':'码农- 研究僧'}})
2. 脚本添加
通过脚本一键修改的方式比较保守保险
主要是终端稍微打错命令回车可能会造成致命错误
具体的脚本只需要连接mongo、查询mongo是否有该数据值(debug)、对应添加新的字段值
(所有的数据中都增加这个字段值)完整代码如下:
import datetime
from django.core.management.base import BaseCommand
# 引入连接mongo的函数,写在外部,就不在代码中放连接函数
from xx.common import get_mongo
class Command(BaseCommand):
def handle(self, *args, **options):
# 连接mongo
mongo = get_mongo()
values = {
"$set": {
"title":"码农- 研究僧"
}
}
mongo["集合"].update({
}, values, multi=True)
# debug查询显示值
for record in mongo["集合"].find():
logger.info(record)
通过python xx.py(文件名)执行该脚本
如果设置了虚拟环境执行脚本
具体可看我这篇文章:
Windows配置虚拟环境以及常用命令(图文解析)
另外一种情况就是:
对应某个数据增加这个字段值的脚本如下:(核心代码)
params = {
"age": 18
}
values = {
"$set": {
"title":"码农- 研究僧"
}
}
mongo["集合"].update(params, values)
# debug查询显示值
for record in mongo["集合"].find():
logger.info(record)
边栏推荐
- Robot framework setting variables
- 1.2 download and installation of the help software rstudio
- Three components of openpyxl
- Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
- Emotional analysis of wechat chat records on Valentine's day based on Text Mining
- How to prepare for the algorithm interview and answer the algorithm interview questions
- Shell script, awk uses if, for process control
- 大约SQL现场“这包括”与“包括在”字符串的写法
- 阿龙的感悟
- 让开发效率飞速提升的跨端方案
猜你喜欢

Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines

Some common processing problems of structural equation model Amos software

Huawei fast game failed to call the login interface, and returned error code -1

Huawei cloud modelarts text classification - takeout comments

Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»

Parker驱动器维修COMPAX控制器维修CPX0200H

R language learning notes

How to view Apache log4j 2 remote code execution vulnerability?

怎么利用Tensorflow2进行猫狗分类识别

Simple interest mode - evil Chinese style
随机推荐
Efficiency difference between row first and column first traversal of mat data types in opencv
MMAP学习
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
Three components of openpyxl
他们主动布局(autolayout)环境的图像编辑器
Teach yourself to train pytorch model to Caffe (2)
Cross end solution to improve development efficiency rapidly
kingbaseES V8R3数据安全案例之---审计记录清除案例
EL与JSTL注意事项汇总
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
怎么利用Tensorflow2进行猫狗分类识别
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
SQL knowledge leak detection
Huawei cloud modelarts text classification - takeout comments
华为云ModelArts文本分类–外卖评论
Deployment of Jenkins under win7
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Advantages of robot framework
Regular expressions and re Libraries