Python3.7.3
pymongo==3.7.2
from pymongo import MongoClient
client =MongoClient('localhost',27017)
db=client.proxy #proxy is a database name of my MongoDB
collection=db.proxytable #proxytable is a collection name of proxy in my MongoDB
for item in collection.find():print(item) #item is each row of data
collection.find_one({"port":"8118"}) #Get the first data with port equal to 8118
for foo in collection.find({"port":"8118"}):print(foo)
# Data with port less than 9000 is sorted by ip
# Because my port in MongoDB stores string type data,So when comparing the size,Than the first character,If it is int type data,Normal comparison
for foo in collection.find({"port":{"$lt":"9000"}}).sort("ip"):print(foo)
collection.count() #Statistics collection data
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 is set to 1 to identify the index in ascending order,-1 descending
collection.remove()
collection.drop()
mongoexport -d test -c users --csv -f name,age -o e:\python\users.csv
Recommended Posts