1. 程式人生 > >mongodb python gridfs.find()條件查詢

mongodb python gridfs.find()條件查詢

https://docs.mongodb.com/manual/reference/operator/query/ 條件查詢的型別說明

查詢的格式

for grid_out in gridFS.find({"filename":{"$regex":"/c/"}}): 這樣可以匹配到所有以c開頭的名稱

但是通常我們查的是id,如果直接這樣查id會沒有結果的,因為再mongodb中,id中一種objectid物件,應該有其他形式,但是樓主沒找到,因此換了一種方式

ID匹配的方式,需要引入 from bson.objectid import ObjectId

但此種方式,不知道如何使用正則(留作以後探索),因此採用第二種方式

for grid_out in gridFS.find({ "_id": ObjectId("5be14a3beefc3218a0812e16")}):

採用where來,取得id物件的字串來進行正則匹配 

for grid_out in gridFS.find({ "$where": "this._id.str.match(/.*5be14/)"}):

對於linux下的系統,去掉str。。耗費2小時找不到,瞎猜的..懵對了。。太難

for grid_out in gridFS.find({ "$where": "this._id.match(/.*5be14/)"}):