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. 【leetcode-73】 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  2. HTTP.SYS远程执行代码漏洞分析 (MS15-034 )

    写在前言:   在2015年4月安全补丁日,微软发布了11项安全更新,共修复了包括Microsoft Windows.Internet Explorer.Office..NET Framework.S ...

  3. Dijkstra和Floyd算法

    #include #include #include #define Infinity 999 //最大值 #define Max_Vertex_Num 20 //顶点数最多为20 #define L ...

  4. 搭建VirtualBox虚拟机集群

    ===============================VirtualBox常用网络===============================NetworkAddress Translati ...

  5. Idea运行web项目时,提示java.lang.ClassNotFoundException: com.mysql.jdbc.Driver解决方法

    今天用 idea写了个工程.结果最后报错,错误信息如下: java.lang.ClassNotFoundException: com.mysql.jdbc.Driverat org.apache.ca ...

  6. sql-leetcode Consecutive Numbers

    开始想 用 group 把Num都聚在一起 -- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.--它的作用是通过一定的规则将一个数据集划分成若干个小 ...

  7. dll和lib的关系(转)

    转自http://blog.163.com/zhengjiu_520/blog/static/3559830620093583438464/ 前面有一章说编译与链接的,说得很简略,其实应该放到这一章一 ...

  8. sql server 横向转丛向及FOR XML PATH使用

    1.开始数据结构如下: 2.转为如下图: 使用如下SQL语句: ---横向转丛向 select name '姓名', max(case when course='语文' then score end) ...

  9. luogu P3238 [HNOI2014]道路堵塞

    传送门 这什么题啊,乱搞就算了,不知道SPFA已经死了吗 不对那个时候好像还没死 暴力就是删掉边后跑Dijkstra SPFA 然后稍微分析一下,可以发现题目中要求的不经过最短路某条边的路径,一定是先 ...

  10. TensorFlow从入门到理解(二):你的第一个神经网络

    运行代码: from __future__ import print_function import tensorflow as tf import numpy as np import matplo ...