mongoengine使用示例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年10月19日
@author: zzy
'''
from mongoengine import * #连接数据库
def lazy_connect():
#mongodb://dbuser:sa123@192.168.1.58:27017/db_datacn
connect('db_datacn', host='mongodb://192.168.1.58/', username='dbuser', password='sa123') lazy_connect() # Defining our documents
# 定义文档user,post,对应集合user,post
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50) #Embedded documents 嵌入的文件,it doesn’t have its own collection in the database
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120) class Post(Document):
title = StringField(max_length=120, required=True)
# ReferenceField相当于foreign key
author = ReferenceField(User, reverse_delete_rule=CASCADE)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
#允许继承
meta = {'allow_inheritance': True} #Post的子类
class TextPost(Post):
content = StringField() #Post的子类
class ImagePost(Post):
image_path = StringField() #Post的子类
class LinkPost(Post):
link_url = StringField() def save_user():
# john = User(email='john@example.com')
# john.first_name = 'john'
# john.last_name = 'cvsh'
# john.save()
#
# ross = User(email='ross@example.com')
# ross.first_name = 'Ross'
# ross.last_name = 'Lawley'
# ross.save()
#
# post1 = TextPost(title='Fun with MongoEngine', author=john)
# post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
# post1.tags = ['mongodb', 'mongoengine']
# post1.save()
#
# post2 = LinkPost(title='MongoEngine Documentation', author=ross)
# post2.link_url = 'http://docs.mongoengine.com/'
# post2.tags = ['mongoengine']
# post2.save() # for post in Post.objects:
# print(post.title)
#
# for post in TextPost.objects:
# print(post.content)
#
# for post in Post.objects:
# print(post.title)
# print('=' * len(post.title))
# if isinstance(post, TextPost):
# print(post.content)
# if isinstance(post, LinkPost):
# print('Link: {}'.format(post.link_url))
Post.objects(tags='mongoengine').update(tags=['abc',''])
for post in Post.objects.all():
#for post in Post.objects(tags='mongoengine'):
print(post.title)
#Post.objects(tags='mongodb').delete() num_posts = Post.objects(tags='mongodb').count()
print('Found {} posts with tag "mongodb"'.format(num_posts)) if __name__ == '__main__':
save_user()
mongoengine使用示例的更多相关文章
- pymongo和mongoengine安装和使用教程 包含常用命令行和代码示例 | pymongo and mongoengine tutorial on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/e88f04e5/,欢迎阅读最新内容! pymongo and mongoengine tutorial on ubuntu 16. ...
- python操作三大主流数据库(9)python操作mongodb数据库③mongodb odm模型mongoengine的使用
python操作mongodb数据库③mongodb odm模型mongoengine的使用 文档:http://mongoengine-odm.readthedocs.io/guide/ 安装pip ...
- python通过mongoengine中connect函数连接多个数据库
mongoengine支持程序同时连接多个数据库,这些数据库可以位于一个或多个mongo之中,通过alias名称区分不同的连接即可. 可以通过switch_db切换到不同的数据库,进行读写操作,swi ...
- 实验2、Flask模板、表单、视图和重定向示例
实验内容 1. 实验内容 表单功能与页面跳转功 能是Web应用程序的基础功能,学习并使用他们能够更好的完善应用程序的功能.Flask使用了名为Jinja2的模板引擎,该引擎根据用户的交互级别显示应用程 ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
随机推荐
- stm32下载程序,拔了调试器不能运行程序解决方案
A:肯定是只拔了仿真器与电脑连接的那端,然后把另外端依然接在板子上.我说的没错吧 B: 对的,这样就会一直复位吗 这是复位的问题,当JLINK在板子上连接的时候,断电情况下,会一直把RESET拉低,导 ...
- mysql忘记root密码的处理方法
在linux下,如果忘记了mysql中root用户的密码可以采用以下办法解决. 1. 修改my.cnf,加入skip-grant-tables 修改mysql的配置文件my.cnf,在[mysqld] ...
- JBPM具体应用之decision节点的使用
JBPM工作流引擎为我们提供了许多的节点应用,每一个节点都有其不同的作用,其中有四个比较常用的节点,他们分别decision,fork,state和task.在本文中我们先介绍decision节点,余 ...
- 转 -ALSA 配置
原文地址:-ALSA 配置">转 -ALSA 配置作者:超级大苹果 alsa 音频路径的问题: 在sound/soc/codecs目录中有很多音频codec的codec驱动,我使用的是 ...
- 如何使用ThinkPHP5 ,自动生成目录?
具体步骤: A.在build.php中按照实际需求修改定义模块的内容: B.修改Public/index.php,在代码中加入: // 读取自动生成定义文件 $build = include '/.. ...
- random和os模块
一.random模块 常用方法如下: #-*- coding:utf-8 -*- import random print(random.randint(1,100)) # 获取一个范围内的随机数,包含 ...
- easylogging++学习记录(一):接入
easylogging++是一个非常轻量级并且非常高效的一个日志库,支持文件配置,支持线程安全,并且其自定义格式非常的方便,最关键的是,其所有代码都集中在一个.h头文件之中,完全不需要引用第三方库,接 ...
- (java基础)抽象类加泛型的理解
今天在群里问了个基础问题,挨喷了..这更加激起了我对知识的渴望.也在此铭记一下,将来有经验了要对刚入门的童鞋们严格点,简单的东西要自己看...唉,程序员何苦为难程序猿呢.. 接下来简单总结下这个万能的 ...
- Unity shader saturate
当你想将颜色值规范到0~1之间时,你可能会想到使用saturate函数(saturate(x)的作用是如果x取值小于0,则返回值为0.如果x取值大于1,则返回值为1.若x在0到1之间,则直接返回x的值 ...
- Codeforces 1142D Foreigner (DP)
题意:首先定义了一种类数(标志数) 1:1到9都是标志数. 2:若x / 10是标志数,假设x /10在标志数中的排名是k, 若x的个位数小于k % 11, 那么x也是标志数. 现在给你一个字符串,问 ...