[PYTHON] Work memorandum (pymongo) Part 1. Basic operation

About this article

I used pymongo at work, so I created this article as a memorandum. I will write it separately below.

  1. Basic operation (this page)
  2. What is MongoDB, What is pymongo
  3. Connection method
  4. Insert method
  5. Search method
  6. Update method
  7. Delete method
  8. Relatively convenient operation (bulk_write) 0. bulk_write
    See also: pymongo doc Bulk_Write
  9. I didn't use it much, but I don't want to check it again, so I'll write it down. 0. aggregate
    See also: MongoDB Document aggregate pipeline

Operating environment

OS: Mac OS
MongoDB: 4.2
Python 3.6.8 :: Anaconda, Inc.
pymongo: 3.9.0

What is MongoDB?

--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.

What is pymongo?

As you can guess from the name, it is a MongoDB library that runs on python.

How to operate pymongo

Basically, make a rough memo by referring to the following. Reference: pymongo documentation

Connect with MongoDB

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

How to insert

--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) 

Search method

――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)

How to delete

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

How to update

--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

Work memorandum (pymongo) Part 1. Basic operation
Work memorandum (pymongo) Part 2. Convenient operation (bulk_write)
Python basic memorandum part 2
Python Basic Memorandum Part 3-About Object Orientation-
Pandas operation memorandum
Django's basic memorandum
Basic operation of pandas
Linux basic command memorandum
Basic operation of Pandas
Python basic memo --Part 2
Installing Python 3 on Mac and checking basic operation Part 1
Python basic memo --Part 1
python memorandum super basic
Python application: Pandas Part 1: Basic
Python Basic Grammar Memo (Part 1)
I wrote the basic operation of Pandas with Jupyter Lab (Part 1)
I wrote the basic operation of Pandas with Jupyter Lab (Part 2)