python操作mongodb数据库
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)