Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding:utf-8
- # @author cloudy <liufuyun88@gmail.com>
- # @created Wed Nov 29 2017 14:58:52 GMT+0800 (CST)
- import mongoengine as me
- me.connect(db="test")
- class User(me.Document):
- email = me.StringField(required=True, unique=True)
- first_name = me.StringField(max_length=50)
- last_name = me.StringField(max_length=50)
- age = me.IntField()
- # validation rule
- def clean(self):
- pass
- class Comment(me.EmbeddedDocument):
- content = me.StringField()
- name = me.StringField(max_length=30)
- class Post(me.Document):
- title = me.StringField(required=True, default="no title")
- author = me.ReferenceField(User, reverse_delete_rule=me.CASCADE)
- tags = me.ListField(me.StringField(max_length=50))
- comments = me.ListField(me.EmbeddedDocumentField(Comment))
- meta = {'allow_inheritance': True, 'collection': 'post'}
- class TextPost(Post):
- content = me.StringField()
- class ImagePost(Post):
- image_path = me.StringField()
- class LinkPost(Post):
- link_url = me.StringField()
- class Article(me.Document):
- tags = me.ListField(me.StringField())
- if __name__ == '__main__':
- # 保存1
- # ross = User(
- # email="test@moresec.cn", first_name="wu", last_name="tian").save()
- # 保存2
- # ross = User(email="test@moresec.cn")
- # ross.first_name = "Ross"
- # ross.last_name = "Lawley"
- # ross.save()
- # ross.first_name = "Like"
- # ross.save()
- # 保存3
- # post1 = TextPost(title='Fun with MongoEngine', author=ross)
- # post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
- # post1.tags = ['mongodb', 'mongoengine']
- # post1.save()
- # 读取
- # for user in User.objects:
- # print type(user.id)
- # for user in User.objects(email='test@moresec.cn'):
- # print user
- # nums
- # print User.objects(email="test@moresec.cn").count()
- # user = User.objects(email="cloudy@moresec.cn").get()
- # print user
- # find
- # print Post.objects(author=user).get()
- # Find all pages Bob authored
- # authors是个list
- # Page.objects(authors__in=[bob])
- # Find all pages that both Bob and John have authored
- # Page.objects(authors__all=[bob, john])
- # Remove Bob from the authors for a page.
- # Page.objects(id='...').update_one(pull__authors=bob)
- # Add John to the authors for a page.
- # Page.objects(id='...').update_one(push__authors=john)
- # User(email="test@moresec.cn", first_name="hello", last_name="fuck").save()
- # me.errors.DoesNotExist
- # 保存时可以避免校验
- # recipient.save(validate=False)
- # print User.objects.first()
- # print User.objects.order_by("+email").first()
- #主键
- # page.id == page.pk
- # field on embedded documents
- # uk_pages = Page.objects(author__country='uk')
- # print User(email="fuck@moresec.cn", first_name="do", last_name="dajo", age=23).save()
- # print User.objects.sum('age')
- # print User.objects.average('age')
- # article = Article()
- # article.tags = ['aaa', 'bbb', 'ccc', 'bb']
- # article.save()
- # 统计词汇云
- # tag_freqs = Article.objects.item_frequencies('tags', normalize=False)
- # print tag_freqs
- # from operator import itemgetter
- # top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10]
- # print top_tags
- # 优化获取
- # user = User.objects.only("email").first()
- # print user.email
- # user = User.objects.exclude('email').first()
- # print user.first_name
- # 间接引用
- post = Post.objects.first()
- print post.author.first_name
- # 这个api还是存疑
- # post = Post.objects.select_related(max_depth=0)[0]
- # print post.author
- # print post.author
- # post = Post.objects.no_dereference().first()
- # print post.author
- # 这个用法也存疑,me 没有no_dereference
- # with me.no_dereference(Post) as Post:
- # post = Post.objects.first()
- # print post.author
Add Comment
Please, Sign In to add comment