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. Markdown Cheatsheet

    This is intended as a quick reference and showcase. For more complete info, see John Gruber's origin ...

  2. Chrome DevTools: Export your raw Code Coverage Data

    The Code Coverage tool visually shows you which lines of code in your CSS and JavaScript are used an ...

  3. Sublime Text 3 浅色主题

    1.参考 如何优雅使用Sublime Text3(Sublime设置豆沙绿背景色和自定义主题) Fluidvision Inspiredgithub LightRays Mac 20Classic P ...

  4. Kibana 搜索语法

    Kibana 搜索语法 Kibana 支持三种搜索语法, 分别是 Lucene query 语法, 基于 json 的 ES query语法, 以及 Kuery 语法. 前两种语法可以直接使用, Ku ...

  5. 记一次ElasticSearch重启之后shard未分配问题的解决

    记一次ElasticSearch重启之后shard未分配问题的解决 环境 ElasticSearch6.3.2,三节点集群 Ubuntu16.04 一个名为user的索引,索引配置为:3 primar ...

  6. bzoj 2726 任务安排(3)/loj 10184-10186 斜率优化

    任务安排1 #include<bits/stdc++.h> #define int long long using namespace std; ; int n,s,t[N],c[N],f ...

  7. Nginx 关闭日志生成文件

    nginx 关闭日志:其实一种方法就是写入/dev/null 文件 或者设置关闭: nginx 日志有两个类型  access.log  http 记录访问日志. error.log   server ...

  8. 牛客寒假算法基础集训营4 G(最小生成树)

    题目链接 题目要求的是得到k种不同的元素,(题解)将每种类型视为一个点,跑一边克鲁斯卡尔即可. #include <set> #include <map> #include & ...

  9. javascript判断节点是否在dom

    在项目中碰到同事写的一段代码: //焦点必须在实时dom树中 if (ele && typeof this.document.contains === "function&q ...

  10. linux find 只获取文件名而去除路径

    find /var/process_log/ -name '*.log' -exec basename {} \;