I used pymongo at work, so I created this article as a memorandum. I will write it separately below.
OS: Mac OS
MongoDB: 4.2
Python 3.6.8 :: Anaconda, Inc.
pymongo: 3.9.0
--The features of MongoDB are as follows. - NoSQL -** JSON format data can be registered without schema settings. ** ** It is convenient because you can insert the Rest API response as it is, or you can register it in the DB even if the schema is different. --Fast writing to DB --Unique processing such as aggregation Since it can store json data, it can also aggregate arrays, but it is more difficult to understand than SQL ... --It may be a unique DB for those who are accustomed to RDB with weak transactions etc.
As you can guess from the name, it is a MongoDB library that runs on python.
Basically, make a rough memo by referring to the following. Reference: pymongo documentation
main.py
import pymongo
from pymongo import MongoClient
#Connection with default parameters(host=localhost,port = 27017)
#We cannot guarantee that you can actually connect
#It seems that an error occurs when reading and writing DB such as find
client = MongoClient() #DB connection()
db = client.DB name#Get DB
collection = db.Collection name#collection(Something like a table)Get
--Insert json format data --The inserted data will be added to the collection with _id added.
main.py
#Insert into collection(Just pass json format data as an argument)
#Data creation
json_list = [{"x":i,"y":i*2,"z":i*3} for i in range(0,5)]
#Insert document or documents(Seems to be deprecated)
collection.insert(json_lsit)
#Insert only one document
collection.insert_one({"x":255,"y":255,"z":255})
#Data creation
json_list = [{"x":i,"y":i*2,"z":i*3,"a":i*4} for i in range(0,5)]
#Insert multiple documents
collection.insert_many(json_list)
――It's a rough image, but it seems that the SQL select clause corresponds to projection and where clause corresponds to filter. See also: Search, View operations (https://docs.mongodb.com/manual/reference/operator/query/)
main.py
#Search all(If you do not specify anything in filter, you can get all items)
data = collection.find(filter={})
for i in data:
pprint.pprint(i)
#Search(Just set the columns you don't need in projection to 0
# 0,It seems that you can turn the display on and off with True or False even if it is not 1.
data = collection.find(filter={"x":{"$gt":1, # $gt:1 ->Greater than 1
"$lt":3}}, # $lt:3 ->Less than 3
projection={"_id":0}
)
for i in data:
pprint.pprint(i)
Conditional expression query seems to be usable in the same way as find
main.py
#Basically just pass the conditional expression
#Delete one document that matches the conditions
collection.delete_one({"x":1})
#Delete documents that match the criteria(Delete documents with x less than 3)
collection.delete_many({"x":{"$lt":3}})
#All items can be deleted without specifying a conditional expression(Just pass an empty dict)
# collection.delete_many({}) #Delete documents that match the criteria
--By specifying upsert, you can also insert a document if the filter condition is not met. See also: Update operations (https://docs.mongodb.com/manual/reference/operator/update/)
main.py
#Update one document that matches the condition specified by filter according to update
collection.update_one(filter,
update,
upsert=False
)
#Update documents that match the conditions specified by filter according to update
collection.update_many(filter,
update,
upsert=False
)
It's a rough explanation, but the basic operation is like this. Next time, if I feel like it, I will write bulk_write, aggregate, etc. more carefully.
Recommended Posts