1. 程式人生 > >python操作mongodb 查詢、更新

python操作mongodb 查詢、更新

我有這樣一個嵌入式文件(python 操作mongoEngine)

    class price2(EmbeddedDocument):
        name = StringField(required=True)
        market = StringField(required=True)
        date = StringField(required=True)
        average = StringField(required=True)
        week_price = StringField(default=None)

    class
price1(Document): price = EmbeddedDocumentField(price2)

我現在需要通過

name = StringField(required=True)
market = StringField(required=True)
date = StringField(required=True)
average = StringField(required=True)

中任意一個欄位(或者其它方法)查詢以及更新,資料庫裡的資料格式如下:

{
    "_id" : ObjectId("59a63fdfac7a4f1740cdbed0"
)
, "price" : { "name" : "白菜", "market" : "成都市...", "date" : "2017-08-30", "average" : "4.00元/公斤", "week_price" : "" } }

各位大神,求幫助啊,謝謝

注:剛開始準備將所有查詢、更新等操作封裝在一個類裡,沒找到解決辦法,不得已改為一下方法:

cate = price2(price_name=name,price_market=market)
result = price1.object.filter(price=cate)

price 是price1中的price欄位

查詢結果轉換成字典:user = User.objects.filter(raw={‘username’: ‘tm’}).first().to_mongo()

MongoDB配置成本地服務:
1、mongo.log
2、mongo.config:
dbpath=D:\mongodb\data\db
logpath=D:\mongodb\log\mongo.log
3、管理員方式啟動命令列(到mongodb 的bin目錄下):
mongod –config D:\mongodb\mongo.config –install –serviceName “MongoDB” –journal
ps:–是兩條英文“-”