python操作三大主流数据库(9)python操作mongodb数据库③mongodb odm模型mongoengine的使用
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的使用的更多相关文章
- python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改、删除操作
python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改.删除操作 项目目录: ├── flask_redis_news.py ├── forms.py ├ ...
- python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用
python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用 redispy安装安装及简单使用:https://github.com/andymccurdy/r ...
- Python操作三大主流数据库☝☝☝
Python操作三大主流数据库☝☝☝ Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数 ...
- Python操作三大主流数据库✍✍✍
Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...
- python操作三大主流数据库(10)python操作mongodb数据库④mongodb新闻项目实战
python操作mongodb数据库④mongodb新闻项目实战 参考文档:http://flask-mongoengine.readthedocs.io/en/latest/ 目录: [root@n ...
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用
python操作mongodb数据库①mongodb的安装和简单使用 参考文档:中文版:http://www.mongoing.com/docs/crud.html英文版:https://docs.m ...
- Python操作三大主流数据库
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...
- python操作三大主流数据库(6)python操作mysql⑥新闻管理后台功能的完善(增、ajax异步删除新闻、改、查)
python操作mysql⑥新闻管理后台功能的完善(增.删.改.查)安装表单验证D:\python\python_mysql_redis_mongodb\version02>pip instal ...
随机推荐
- SpringBoot集成原生redis
redis的使用之一是Spring-data-redis,前面有介绍. 本篇介绍原生redis也就是jedis.这个效率更高 1.maven引入依赖 <!--springBoot-->&l ...
- [Android] Android v4包CompoundButtonCompatLollipop.class重复问题
用 Butter Knife 8.8.1 导致v4包CompoundButtonCompatLollipop.class重复问题 详细错误如下: Error:Execution failed fo ...
- Kudu系列: Kudu主键选择策略
每个Kudu 表必须设置Pimary Key(unique), 另外Kudu表不能设置secondary index, 经过实际性能测试, 本文给出了选择Kudu主键的几个策略, 测试结果纠正了我之前 ...
- eclipse竖向选择快捷键
eclipse的编辑器自带竖向选择功能,在mac上的开启和关闭方法都是command+option+a,在windows下则为alt+shift+a
- 群发技术-使用python3给微信好友群发消息
本文介绍如何给个人微信好友群发消息. 微信个人号中的群发助手可以一次给30个发送消息,如果要给所有所有群发,则需要自己手动发送多次,或者借助程序实现了.本文即是程序实现教程 一.原理 在微信的官方网站 ...
- IL 学习笔记
先上几篇博客链接: 一步步教你读懂NET中IL(图文详解) C#基础之IL 详解.NET IL代码 C# IL DASM 使用 你必须知道的.NET <C# to IL>.<Expe ...
- Inline Route Constraints in ASP.NET Core MVC
原文 ASP.NET MVC5和Web API2的一个新特性是attribute routing, 通过它我们可以使用[Route]来定义路由模板: public class MessagesCont ...
- String,StringBuilder,StringBuffer区别
一.String,StringBuilder,StringBuffer的大概了解 大家知道String,StringBuilder,StringBuffer三个的基本应用场景. String会一直创建 ...
- 什么是IO多路复用
先百度或者知乎,找到这篇文章 [1] IO 多路复用是什么意思? 文中提到: 第一种好理解,就是来一个请求,fork一个进程,第二种提到I/O多路复用使用单个线程实现的,作者肯定没有写错,因为后面的文 ...
- PHP7语法知识(四):目录文件操作、Cookie与Session、MySQL数据库的使用、Redis数据库、PHP处理XML与JSON
目录文件操作 一.目录 1.判断文件类型: 2.创建和删除目录: 3.打开读取和关闭目录 4.获得路径中目录部分 5.目录磁盘空间 二.文件操作 1.打开文件: 2.读取文件: 3.获得文件属性: 4 ...