#!/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使用示例的更多相关文章

  1. pymongo和mongoengine安装和使用教程 包含常用命令行和代码示例 | pymongo and mongoengine tutorial on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/e88f04e5/,欢迎阅读最新内容! pymongo and mongoengine tutorial on ubuntu 16. ...

  2. python操作三大主流数据库(9)python操作mongodb数据库③mongodb odm模型mongoengine的使用

    python操作mongodb数据库③mongodb odm模型mongoengine的使用 文档:http://mongoengine-odm.readthedocs.io/guide/ 安装pip ...

  3. python通过mongoengine中connect函数连接多个数据库

    mongoengine支持程序同时连接多个数据库,这些数据库可以位于一个或多个mongo之中,通过alias名称区分不同的连接即可. 可以通过switch_db切换到不同的数据库,进行读写操作,swi ...

  4. 实验2、Flask模板、表单、视图和重定向示例

    实验内容 1. 实验内容 表单功能与页面跳转功 能是Web应用程序的基础功能,学习并使用他们能够更好的完善应用程序的功能.Flask使用了名为Jinja2的模板引擎,该引擎根据用户的交互级别显示应用程 ...

  5. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  6. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  7. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  8. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  9. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

随机推荐

  1. Linux批量“解压”JAR文件

    当你需要”解压“很多jar文件时,可以通过很多方式进行,比如下面这种 1,列出每一个jar文件名,逐个展开 for i in $(ls *sour*.jar);do jar xvf $i;done

  2. leetcode427

    本题不会做,从网上找到了python3的解法,记录如下. class Solution: def construct(self, grid): def dfs(x, y, l): if l == 1: ...

  3. 循序渐进Python3(十三) --0-- django之form表单

    django为我们提供了form表单验证功能,下面来学习一下: 武sir博客:http://www.cnblogs.com/wupeiqi/articles/5246483.html  创建了djan ...

  4. 16-EasyNetQ之自动订阅者

    EasyNetQ v0.7.1.30版本有了一个简单AutoSubscriber.你能够使用它很容易的去扫描指定程序集中实现了IConsume 或 IConsumeAsync接口的类,然后这个自动订阅 ...

  5. Spring整合web项目原理

     

  6. 4-1 线程安全性-原子性-atomic-1

    我们发现在不做任何同步的情况下,我们计算的累加结果是错误的. com.mmall.concurrency.example.count.CountExample2 C:\Users\ZHONGZHENH ...

  7. weblogic在linux服务器上部署应用

    SSH软件连接服务器: 服务器地址:xxx.xxx.xxx.40 用户名:xxxx 密码:xxxx 新建文件夹,用来放新代码版本,后面为代码版本号 路径:/home/weblogic 命令:mkdir ...

  8. 场景中,并没有灯源的存在,但是cube却会有灯光照射的反应,这就是Light Probe Group的作用。

    http://blog.csdn.net/qq617119142/article/details/41674755

  9. PHP数组的详细解读

    数组的定义 数组的本质是管理和操作一组变量,数组中可以存储任意长度的数据,也可以存储任意类型的数据.数组中的单元称为元素,每个元素包括下标(键)和值,访问元素的时候,是通过下标来访问,包括一维数组,二 ...

  10. c语言学习笔记-if语句块一定要加分号

    if(a>6) printf("hello");//语句1 printf("world");//语句2 当a>6的时候,执行的分支语句是语句1,而不 ...