MongoDB usage in Python

MongoDB for Python

#1 surroundings##

Python3.7.3
pymongo==3.7.2

#1 ready##

from pymongo import MongoClient

#1.1 Link to MongoDB

client =MongoClient('localhost',27017)

#1.2 Link database###

db=client.proxy #proxy is a database name of my MongoDB

#1.3 Connection set (table name)

collection=db.proxytable #proxytable is a collection name of proxy in my MongoDB

#2 operating##

#2.1 Find all data in the collection###

for item in collection.find():print(item) #item is each row of data

#2.2 Find a single data in the collection###

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

#2.3 Insert data into the collection###

collection.insert({ip:'122.235.240.108',pory:8989})

#2.4 Update the data in the collection, the first brace is the update condition, and the second brace is the content after the update

collection.update({ip:'122.235.240.108'},{port:'8980'})

#2.5 Add index

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

#2.6 Delete all data in collection###

collection.remove()

#2.7 Delete collection###

collection.drop()

#2.8 Data output###

mongoexport -d test -c users --csv -f name,age  -o e:\python\users.csv


Recommended Posts

MongoDB usage in Python
The usage of wheel in python
Detailed usage of dictionary in Python
Usage of os package in python
The usage of tuples in python
Functions in python
The usage of Ajax in Python3 crawler
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Python advanced usage summary
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Python high-order function usage summary!
Talking about inheritance in Python
Noteworthy update points in Python 3.9
Containerize Python applications in 3 minutes
What is object-oriented in python
Generators and iterators in Python
Python high-order function usage summary!
Talking about strings in Python
Summary of logarithm method in Python
Analysis of usage examples of Python yield
Use nohup command instructions in python
What is list comprehension in python
Is there function overloading in python
Python novice learns to raise usage
Detailed sorting algorithm (implemented in Python)
How to wrap in python code
What does rc1 mean in python
Common errors and solutions in python
Use of numpy in Python development
What does def in python do
How to omit parentheses in Python
How to write classes in python
Mongodb and python interaction of python crawler
​What are the numbers in Python?
How to read Excel in Python
There are several keywords in Python
Everything in Python is an object
Simple usage of python definition class
How to view errors in python
What does np do in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
Python learning os module and usage
Description of in parameterization in python mysql
Understanding the meaning of rb in python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
Implementation of JWT user authentication in python