python操作mongodb資料庫

我の前端日记發表於2024-10-23

1.連線

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
print(myclient.list_database_names())

2.查詢

查詢全部

for row in myAuthors.find():
    print(row)
displayName Fay Chang 

按條件查詢:

pub_papers下的publishedDate中含有relational databases
pub_papers下的description中含有relational databases
import regex
r1 = mycol.find( {
   "pub_papers.publishedDate":{ "$regex": "relational databases"  } 
    })
r2 = mycol.find( {
   "pub_papers.description":{ "$regex": "relational databases"  } 
    })
for row in r1:
    print(row)

for row in r2:
    print(row)

相關文章