python操作mongodb数据库③mongodb odm模型mongoengine的使用

文档:http://mongoengine-odm.readthedocs.io/guide/

安装
pip install mongoengine

连接mongodb

方式1:简写
connect('students')
>>> from mongoengine import connect
>>> connect('students')
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary()) 方式2:指定端口和地址
connect('students',host='192.168.3.96',port=) 方式3:使用URI
connect('students',host='mongodb://localhost/students')

示例代码,使用mongoengine操作mongodb数据库

#coding:utf-8
from mongoengine import connect, Document, EmbeddedDocument, DynamicDocument, StringField, IntField,\
FloatField, ListField, EmbeddedDocumentField connect('students') SEX_CHICES = (
('male','男'),
('female','女')
) class Grade(EmbeddedDocument):
''' 成绩 '''
name = StringField(required=True)
score = FloatField(required=True) # class Student(Document):
class Student(DynamicDocument):
'''学生'''
name = StringField(max_length=32, required=True)
age = IntField(required=True)
sex = StringField(choices=SEX_CHICES, required=True)
grade = FloatField()
address = StringField()
grades = ListField(EmbeddedDocumentField(Grade)) meta = {
'collection': 'students',
# 排序功能,按照分数倒序
'ordering':['-grade']
} class TestMongoEngine(object):
def add_one(self):
'''添加一条数据到数据库'''
yuwen = Grade(
name = '语文',
score = 90)
shuxue = Grade(
name = '数学',
score = 100)
stu_obj = Student(
name = '张三丰',
age = 15,
grades = [yuwen, shuxue],
sex = 'male'
)
# 直接添加remark字段是无法添加成功的,需要引入动态添加字段的方法DynamicDocument
     
stu_obj.remark = 'remark'
stu_obj.save()
return stu_obj def get_one(self):
''' 获取单条数据 '''
return Student.objects.first() def get_more(self):
''' 获取多条数据 '''
# return Student.objects
return Student.objects.all() def get_one_from_oid(self, oid):
''' 查询指定id的数据 '''
return Student.objects.filter(id=oid).first() def update(self):
''' 修改数据 '''
# 修改一条数据
# res = Student.objects.filter(sex='male').update_one(inc__age=1)
# return res # 修改多条数据
res = Student.objects.filter(sex = 'male').update(inc__age=10)
return res def delete(self):
''' 删除数据 '''
# 删除一条数据
# res = Student.objects.filter(sex='male').first().delete()
# return res # 删除多条数据
res = Student.objects.filter(gender='male').delete() def main():
en = TestMongoEngine()
# en.add_one() # res = en.get_one()
# print(res.name) # rows = en.get_more()
# for row in rows:
# print(row.name) # res = en.get_one_from_oid('5a9df2e48a86b467d4a2c44f')
# print(res.name) # res = en.update()
# print(res) res = en.delete()
print(res) if __name__ == "__main__":
main()

python操作三大主流数据库(9)python操作mongodb数据库③mongodb odm模型mongoengine的使用的更多相关文章

  1. python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改、删除操作

    python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改.删除操作 项目目录: ├── flask_redis_news.py ├── forms.py ├ ...

  2. python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用

    python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用 redispy安装安装及简单使用:https://github.com/andymccurdy/r ...

  3. Python操作三大主流数据库☝☝☝

    Python操作三大主流数据库☝☝☝ Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数 ...

  4. Python操作三大主流数据库✍✍✍

    Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...

  5. python操作三大主流数据库(10)python操作mongodb数据库④mongodb新闻项目实战

    python操作mongodb数据库④mongodb新闻项目实战 参考文档:http://flask-mongoengine.readthedocs.io/en/latest/ 目录: [root@n ...

  6. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  7. python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用

    python操作mongodb数据库①mongodb的安装和简单使用 参考文档:中文版:http://www.mongoing.com/docs/crud.html英文版:https://docs.m ...

  8. Python操作三大主流数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库:  ...

  9. python操作三大主流数据库(6)python操作mysql⑥新闻管理后台功能的完善(增、ajax异步删除新闻、改、查)

    python操作mysql⑥新闻管理后台功能的完善(增.删.改.查)安装表单验证D:\python\python_mysql_redis_mongodb\version02>pip instal ...

随机推荐

  1. HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...

  2. Openresty 学习笔记(二)Nginx Lua 正则表达式相关API

    ngx.re.match 语法: captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) 环境: init_w ...

  3. Spark RDD基本概念与基本用法

    1. 什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合.RDD具 ...

  4. Groovy 设计模式 -- Strategy 模式

    策略模式 https://en.wikipedia.org/wiki/Strategy_pattern In computer programming, the strategy pattern (a ...

  5. seleniums私房菜系列一 ---- selenium简介

    一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...

  6. 多线程this逃逸

    this逃逸,  是指在构造函数返回之前,其它线程就持有该对象的引用,调用尚未构造完全的对象的方法,可能引发令人疑惑的错误,应该避免this逃逸事件的发生. this逃逸经常发生在构造函数中启动线程或 ...

  7. GoogleNet

    NET-IN-NET 采用net-in-net 结构(不使用传统线性卷积,使用Mlpconv) 采用全局均值池化来提高传统CNN 网络中最后全连接层参数过于复杂的特点.(全连接层造成网络泛化能力差,a ...

  8. node.js 环境

    Centos 7.2 安装 Node.js 环境 Node.js 是运行在服务端的 JavaScript, 是基于 Chrome JavaScript V8 引擎建立的平台. 1. Node.js w ...

  9. Burp Suite Intruder中爆破模式介绍

    Burp Suite Intruder中爆破模式介绍 - Introduction to Burst Mode in Burp Suite Intruder 1.sniper模式  使用单一的Payl ...

  10. Coursera, Deep Learning 1, Neural Networks and Deep Learning - week1, Introduction to deep learning

    整个deep learing 系列课程主要包括哪些内容 Intro to Deep learning