Python3.7.3
pymongo==3.7.2from pymongo import MongoClientclient = MongoClient('localhost',27017)db=client.proxy # proxy是我的MongoDB的一个数据库名collection=db.proxytable # proxytable是我的MongoDB中proxy的一个集合名for item in collection.find():
print(item) # item 就是每一行数据 
collection.find_one({"port":"8118"}) # 获取port等于8118的第一条数据
for foo in collection.find({"port":"8118"}):
print(foo)
# port小于9000的数据按ip排序
# 因为我的MongoDB中port存的是string类型数据,所以比较大小时,比的是第一个字符,如果是int类型数据,正常比较
for foo in collection.find({"port":{"$lt":"9000"}}).sort("ip"):
print(foo)
collection.count() # 统计集合数据条数collection.insert({ip:'122.235.240.108',pory:8989})collection.update({ip:'122.235.240.108'},{port:'8980'})from pymongo import ASCENDING, DESCENDING
users.create_index([("ip", DESCENDING), ("port", ASCENDING)])
# ASCENDING 设为1 标识索引升序,-1降序collection.remove()collection.drop()mongoexport -d test -c users --csv -f name,age -o e:\python\users.csv