標籤:等於 doc import sum imp 英國 count() 字串 border
1. 擷取資料
for post in Post.objects: print(post.title) 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))
#
展示資料for post in Post.objects: print(post.title) 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))
# 擷取特定的資料內容for post in Post.objects(tags=‘mongodb‘): print(post.title) # 擷取特定的資料內容的數量num_posts = Post.objects(tags=‘mongodb‘).count()print(‘Found {} posts with tag "mongodb"‘.format(num_posts)) # 作者的國家(外鍵)uk_pages = Page.objects(author__country=‘uk‘) - ne - 不相等- lt - 小於- lte - 小於等於- gt - 大於- gte - 大於等於- not - 取反Q(age__not__mod=5)- in - 值在列表中- nin - 值不在列表中- mod - 模數(餘)- all - 與列表的值相同- size - 數組的大小- exists - 欄位的值存在 young_users = Users.objects(age__lte=18)
字串查詢 - exact – 字串型欄位完全符合這個值- iexact – 字串型欄位完全符合這個值(大小寫敏感)- contains – 字串欄位包含這個值- icontains – 字串欄位包含這個值(大小寫敏感)- startswith – 字串欄位由這個值開頭- istartswith – 字串欄位由這個值開頭(大小寫敏感)- endswith – 字串欄位由這個值結尾- iendswith – 字串欄位由這個值結尾(大小寫敏感)- match – 執行 $elemMatch 操作,所以你可以使用一個數組中的 document 執行個體 users = User.objects[10:15]
users = User.objects.skip(10).limit(5) 求和:yearly_expense = Employee.objects.sum(‘salary‘) 求平均數:mean_age = User.objects.average(‘age‘) 求包含了哪些年齡(去重)all_age = User.objects.distinct(‘age‘) 按條件去除某些(不包含年齡小於18的)User.objects.exclude(age__lt=18) 只擷取一個欄位User.objects.only(‘age‘) User.objects(age=12).only(‘name‘).first() # 定義查詢方法class BlogPost(Document):
title = StringField()
published = BooleanField()
@queryset_manager
def live_posts(doc_cls, queryset):
return queryset.filter(published=True)BlogPost(title=‘test1‘, published=False).save()BlogPost(title=‘test2‘, published=True).save()assert len(BlogPost.objects) == 2assert len(BlogPost.live_posts()) == 1 # 只擷取想要的欄位資料>>> class Film(Document):... title = StringField()... year = IntField()... rating = IntField(default=3)...>>> Film(title=‘The Shawshank Redemption‘, year=1994, rating=5).save()>>> f = Film.objects.only(‘title‘).first()>>> f.title‘The Shawshank Redemption‘>>> f.year # None>>> f.rating # default value3
進階查詢 有時需要將多個條件進行組合,前面提到的方法就不能滿足需求了。這時可以使用MongoEngine的Q類。它可以將多個查詢條件進行 &(與) 和 |(或) 操作。例如下面的語句是查詢所有年齡大於等於18歲的英國使用者,或者所有年齡大於等於20歲的使用者。 from mongoengine.queryset.visitor import Q User.objects((Q(country=‘uk‘) & Q(age__gte=18)) | Q(age__gte=20))
# 排序from datetime import datetime
class BlogPost(Document):
title = StringField()
published_date = DateTimeField()
meta = {
‘ordering‘: [‘-published_date‘]
}blog_post_1 = BlogPost(title="Blog Post #1")blog_post_1.published_date = datetime(2010, 1, 5, 0, 0 ,0) blog_post_2 = BlogPost(title="Blog Post #2")blog_post_2.published_date = datetime(2010, 1, 6, 0, 0 ,0) blog_post_3 = BlogPost(title="Blog Post #3")blog_post_3.published_date = datetime(2010, 1, 7, 0, 0 ,0)
blog_post_1.save()blog_post_2.save()blog_post_3.save()
# get the "first" BlogPost using default ordering# from BlogPost.meta.orderinglatest_post = BlogPost.objects.first()assert latest_post.title == "Blog Post #3"# override default ordering, order BlogPosts by "published_date"first_post = BlogPost.objects.order_by("+published_date").first()
django結合mongoengine實現對mongodb的操作(二)