您好,登錄后才能下訂單哦!
這篇文章主要介紹了在Python中使用MongoEngine操作數(shù)據(jù)庫教程實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
pymongo來操作MongoDB數(shù)據(jù)庫,但是直接把對于數(shù)據(jù)庫的操作代碼都寫在腳本中,這會讓應(yīng)用的代碼耦合性太強,而且不利于代碼的優(yōu)化管理
一般應(yīng)用都是使用MVC框架來設(shè)計的,為了更好地維持MVC結(jié)構(gòu),需要把數(shù)據(jù)庫操作部分作為model抽離出來,這就需要借助MongoEngine
MongoEngine是一個對象文檔映射器(ODM),相當于一個基于SQL的對象關(guān)系映射器(ORM)
MongoEngine提供的抽象是基于類的,創(chuàng)建的所有模型都是類
安裝
pip install mongoengine
使用時先聲明一個繼承自MongoEngine.Document的類
在類中聲明一些屬性,相當于創(chuàng)建一個用來保存數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),即數(shù)據(jù)已類似數(shù)據(jù)結(jié)構(gòu)的形式存入數(shù)據(jù)庫中,通常把這樣的一些類都存放在一個腳本中,作為應(yīng)用的Model模塊
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) users = Users.objects.all() #返回所有的文檔對象列表 for u in users: print("name:",u.name,",age:",u.age)
保存文檔
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) user1 = Users( name='zz', age= 11 ) user1.save() print(user1.name) user1.name = 'zz11' user1.save() print(user1.name)
查詢10=<年齡<30的,按姓名排列
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) user_search = Users.objects(age__gte=10, age__lt=33).order_by('name') for u in user_search: print("name:",u.name,",age:",u.age)
查詢10=<年齡<30的,按姓名倒序
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) user_search = Users.objects(age__gte=10, age__lt=33).order_by('-name') for u in user_search: print("name:",u.name,",age:",u.age)
查詢name=zz11
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) tmp = Users.objects(name="zz11") for u in tmp: print("name:",u.name,",age:",u.age)
修改name=zz11的age加1
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) tmp = Users.objects(name="zz11").update(inc__age=1) tmp = Users.objects(name="zz11") for u in tmp: print("name:",u.name,",age:",u.age)
修改name=zz11的age設(shè)為55
from mongoengine import * connect('test', host='localhost', port=27017) import datetime class Users(Document): name = StringField(required=True, max_length=200) age = IntField(required=True) tmp = Users.objects(name="zz11").update(set__age=55) tmp = Users.objects(name="zz11") for u in tmp: print("name:",u.name,",age:",u.age)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。