亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 BSON (Python) 檢索從 MongoDB 存儲的數據?

如何使用 BSON (Python) 檢索從 MongoDB 存儲的數據?

搖曳的薔薇 2023-12-09 15:38:13
到目前為止,在我的代碼中,我成功地將數據存儲到 mongoDB 中。現在我希望能夠檢索我存儲的數據。正如你所看到的,我一直在嘗試,但不斷出現錯誤。使用 BSON 時,我是否必須首先解碼數據才能從 mongoDB 檢索數據?任何幫助將不勝感激?。▽靵y的代碼表示歉意,我只是通過反復試驗進行練習)import jsonfrom json import JSONEncoderimport pymongo from pymongo import MongoClientfrom bson.binary import Binaryimport pickle#Do this for each client = MongoClient("localhost", 27017)db = client['datacampdb']coll = db.personpractice4_collection  #creating a collection in the database #my collection on the database is called personpractice4_collection class Person:    def __init__(self, norwegian, dame, brit, german, sweed):        self.__norwegian = norwegian        self.__dame = dame        self.__brit = brit        self.__german = german #private variable         self.__sweed = sweed    # create getters and setters later to make OOP personone = Person("norwegian", "dame", "brit", "german","sweed")  class PersonpracticeEncoder(JSONEncoder):         def default(self, o):            return o.__dict__    #Encode Person Object into JSON"personpracticeJson = json.dumps(personone, indent=4, cls=PersonpracticeEncoder)practicedata = pickle.dumps(personpracticeJson)coll.insert_one({'bin-data': Binary(practicedata)})#print(personpracticeJson)#print(db.list_collection_names()) #get then names of my collections in DB #retriving data from mongodb #Retrieving a Single Document with find_one()print(({'bin-data': Binary(practicedata)}).find_one()) #not working 
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

find_one應該在集合上調用該方法

{'bin-data': Binary(practicedata)}是查找文檔的查詢

coll.find_one({'bin-data': Binary(practicedata)})

女巫的意思是:在集合中查找一個文檔coll,其中bin-data等于Binary(practicedata)


查看完整回答
反對 回復 2023-12-09
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號