Wechat official account: Operation and maintenance development story, author: Double Dong

In most projects, a programming language is required to operate databases. Using Python to operate databases has natural advantages because Python dictionaries and MongoDB documents are almost the same format. This article will introduce how to use Python to operate MongoDB

1 Connecting a Database

1.1 install PyMongo

Using Python to operate MongoDB requires the use of a third-party library, PyMongo. To install this library, use PIP as you would any other Python third-party library:

python3 -m pip install pymongo

Copy the code

You can also specify the installed version:

Python3 -m pip3 install pymongo==3.5.1Copy the code

Update pymongo command:

python3 -m pip3 install --upgrade pymongo

Copy the code

Once installed, open the Python interactive environment and import PyMongo. If no error is reported (as shown in the figure), the installation is successful

image.png

1.2 Connecting a Database

To operate MongoDB with PyMongo, you first need to initialize the database connection.

  • (1) If MongoDB is running on the local computer, and the port has not been changed or the user name and password have not been added, then the MongoClient instance initialization does not need to take parameters, and directly write the following format:
import pymongo
conn = pymongo.MongoClient()

Copy the code
  • (2) If MongoDB runs on other servers, use the UniformResource Identifier (URI) to specify the link address
MongoClient import pymongo conn = pymongo.MongoClient('mongodb://test:[email protected]:27019')Copy the code

The URI format of the MongoDB service is as follows: MongoDB :// Username: password @Server IP address or domain name: port For example:

  • (3) If permission authentication is not configured, the user name and password are not required
The import pymongo conn = pymongo. MongoClient (' mongo: / / 45.10.110.77:27019 ')Copy the code

1.3 Connecting libraries and collections

PyMongo connects libraries to collections in two ways

  • Method 1

Method 1 for connecting databases to collections

from pymongo import MongoClient conn = MongoClient() databae = conn. Database name Collection = database. collection nameCopy the code

Note that in this way, the “database name” and “collection name” in the code are not variable names; they are simply the name of the library and the collection. For example, to connect to the collection example_data_1, the Python code looks like this:

from pymongo import MongoClient
conn = MongoClient()
database = conn.chapter_1
collection = database.example_data_1

Copy the code
  • Way 2

Connect database to collection method 2

from pymongo import MongoClient db_name = 'chapter_1' collection_name = 'example_data_1' conn = MongoClient() database =  conn[db_name] collection = database[collection_name]Copy the code

In this way, variables can be filled directly into square brackets to specify the library and collection names. Of course, you can also fill in a string directly, for example:

from pymongo import MongoClient
conn = MongoClient()
database = conn['chapter_1']
collection = database['example_data_1']

Copy the code

Method 1 and method 2 have exactly the same effect. You can choose whichever way you like.

Mode 2 is mainly used when databases need to be operated in batches. For example, in a project where you sometimes have multiple test environments and now need to update their databases at the same time, approach 2 can be used. This allows you to store the names of multiple databases or collections in a list and then use a loop, as shown below:

database_name_list = ['develop_env_alpha','develop_env_beta','develop_env_preflight'] for each_db in database_name_list:  database = conn[each_db] collection = database.account collection.updateMany(.....)Copy the code

The third line of code in the loop connects to a different library at a time, so that you can update the information of multiple databases at the same time. For multiple collections in the same database, you can also use this method.

Note: In MongoDB, collections are created only after content is inserted! That is, a collection (table) is created before a document (record) is inserted.

2 Method of the MongoDB command in Python

Once you get the collection connection object, you can manipulate MongoDB with its methods.

Although MongoDB commands and collection method names are written in slightly different ways, most MongoDB statements can use arguments copied directly into Python code.

MongoDB commands use camel name, while PyMongo uses “lowercase and underscore”. Their comparison is shown in the table below:

Directing a command PyMongo method
insertOne insert_one
insertMany insert_many
find find
updateone update_one
updateMany update_many
deleteOne delete_one
deleteMany delete_many

For example, Robo 3T executes a batch insert statement:

The getCollection (' example_data_1). InsertMany ([{' name ':' Zhao Xiaosan ', 'age: 20,' address ':' Beijing '}, {" name ": Small four 'money', 'age: 21,' address ':' Shanghai '}, {' name ':' sun small five ', 'age: 20,' address ':' shandong '}, {" name ": 'small six li', 'age: 23,' address ':' hebei '}, {' name ':' ouyang seven ', 'age: 24,' address ':' hangzhou}])Copy the code

Use Python to batch insert data as follows:

from pymongo import MongoClient conn = MongoClient() database = conn.chapter_1 collection = database.example_data_2 Collection. Insert_many ([{' name ':' wang2 xiao3 er4 ', 'age: 21,' student: True, 'address' : 'guangzhou'}, {" name ": 'Zhao Xiaosan', 'age: 20,' student: True, 'address' : 'Beijing'}, {' name ':' money small four ', 'age: 21,' student: True, 'address' : 'Shanghai'}, {" name ": 'small five sun', 'age: 20,' student: True, 'address' : 'shandong'}, {' name ':' small six li ', 'age: None,' student: True, 'address' : 'hebei'}, {" name ": 'ouyang seven', 'age: 24,' student ': False,' address ':' hangzhou '}, {' name ':' sun small 8 ', 'age: 25,' student ': False,' address ':' guangzhou}])Copy the code

The new collection name is used in line 4 to make the distinction.

Another benefit of using Python with MongoDB is that if the library or collection currently in use does not exist, PyMongo will automatically create the corresponding library or collection after the insert method is called.

In short, most operations can be copied directly from Robo 3T to Python and run with little modification.

3 Insert data into MongoDB

The basic grammar

Collection. insert_one(dictionary) # Insert a single piece of dataCopy the code

The format of the data to be inserted

{'field_1': value_1, 'field_2': value_2}
[
 {'field_1': value_1, 'field_2': value_2},
 {'field_1': value_3, 'field_2': value_4}
]

Copy the code

instructions

  • MongoDB does not need to create a database in advance, do not need to create collections in advance, do not need to extract and define the data format, want to insert any data, just insert.

  • Dictionaries can be different for different rows of data in the same set

For example,

In Python, insert the dictionary {‘name’: ‘Wang Xiaoliu’, ‘age’: 25, ‘work’: ‘cook’} into MongoDB.

Specific commands are as follows:

Collection. Insert_one ({' name ':' king 6 ', 'age: 25,' the work ':' cook '})Copy the code

Tip: PyMongo also has a generic method — collection.insert().

  • If a dictionary is passed in, collection.insert() is equivalent to insert_one

  • If a collection containing dictionaries is passed in, collection.insert() is equivalent to insert_many

However, the PyMongo developers are ready to remove it, so it is not recommended for use in a formal environment.

4 Query the data in MongoDB

Query a piece of data

We can use the find_one() method to query a single piece of data in the collection, the first piece of data in the example_data_2 document:

from pymongo import MongoClient db_name = 'chapter_1' collection_name = 'example_data_1' conn = MongoClient() database =  conn[db_name] collection = database[collection_name] x=collection.find_one({}) print(x)Copy the code

Output result:

image.png

Query all data in the collection

from pymongo import MongoClient db_name = 'chapter_1' collection_name = 'example_data_1' conn = MongoClient() database =  conn[db_name] collection = database[collection_name] collection = database.example_data_2 rows = collection.find() for row in rows: print(row)Copy the code

The results are as follows:

image.png

Logical query

Collection. The find ({' field name: {' basic symbol: boundary value, 'basic symbol: boundary value}})Copy the code

In Python, query MongoDB for all records whose age is greater than 21 but less than 25, and whose name is not xiahou Xiao Qi.

collection = database.example_data_2 rows = collection.find({'age':{'$lt':25,'$gt':21}, 'name' : {' $ne ':' sun small eight '}}) for the row in rows: print (row)Copy the code

The running effect is as shown in the figure:

image.png

Query and count the results

collection.find().count()

Copy the code

Query and count the results

Collection.find ().sort(' collection.find().sort({' collection.find ': sort}))Copy the code

1 indicates ascending, and **-1** indicates descending

Deduplicate fields

Select * from 'id'; select * from 'id'; select * from 'id'; select * from 'id'; select * from 'id';Copy the code

Note: If the number of fields is large, it is easy not to execute directly in Robo 3T, otherwise Robo 3T May be stuck

5 Update or delete the MongoDB data

The basic grammar

Collection. update_one($set, {'$set': update_one))Copy the code

The updated data

{'field_1': value_1, 'field_2': value_2}

Copy the code

For example,

Updating and deleting data in Python:

  • (1) For the record whose “name” is “Gongsun Xiaobai”, update “age” to 80 and “address” to “America”.

  • (2) Delete the data whose age is 0

1. Update data in MongoDB

In Python, you can use the udate_many method to update data in batches

Collection. Update_many ({' name ':' sun small eight '}, {' $set: {' address ':' the UK ', 'age: 80}})Copy the code

The update operation also supports a “upsert” parameter. This parameter updates data if it exists. If the data does not exist, create one.

For example, for records where “name” is “invisible person”, change “age” to 0 and “address” to “inner world”

Because this record is not present in example_data_1, a direct update will report an error, as shown in the figure.

Result = collection. Update_one ({" name ":" invisible man "}, {' $set: {" name ":" invisible man ", "age" : 0, 'address' : 'the world'}}) print (list (result)Copy the code

image.png

Add the “upsert” parameter and see what happens

Result = collection. Update_one ({" name ":" invisible man "}, {' $set: {" name ":" invisible man ", "age" : 0, 'address':' id ', upsert = True) print(result)Copy the code

image.png

Tip: If update or insert is enabled, the value of “$set” is the entire document content, and should include every field, not just the fields that need to be updated, otherwise only the fields that need to be updated are inserted.

2. Delete data in MongoDB

The basic grammar
Delete collection.delete_one(select collection.delete_many(select collection.delete_many))Copy the code

Delete data whose age is 0. Delete statement as follows:

collection.delete_many({'age': 0})

Copy the code

You are advised to write a query statement, confirm that the queried data is the data you want to delete, and change the find keyword to delete_ONE or delete_many

6 Perform operations that are not common to MongoDB and Python

For the most part, command parameters in MongoDB are copied directly into Python for use, with some exceptions. Suppose the data set example_data_2 looks like this:

image.png

6.1 a null value

In MongoDB, a null value is written as null; in Python, a null value is written as None.

MongoDB does not recognize None,Python does not recognize NULL.

To query all data with empty age from dataset example_data_2, the query statement in Robo 3T is:

db.getCollection('example_data_2').find({'age': null})

Copy the code

The running result is as shown in the figure:

image.png

If you run this query directly into Python, you will get an error, as shown in the following figure:

image.png

Python treats null as a normal variable, but the variable is undefined, causing an error.

In Python, to query for null values, you need to use None. Make a few changes to the above code — change “null” to “None” and the query succeeds, as shown in the figure below:

image.png

6.2 Boolean value

Boolean values are true and false values. In MongoDB, “true” is true, “false” is false, and the first letter is lowercase; In Python, “True” is True, “False” is False, and the first letter is capitalized.

In MongoDB, query all records where student is true, as shown in the figure below:

image.png

Copying the query argument directly into Python will also result in an error, as Python treats true as a normal variable, as shown in the following figure:

image.png

If true is changed to true, the query succeeds, as shown in the figure:

image.png

6.3 Sorting Parameters

Sorting the result of a query is a common operation. In MongoDB, sort() takes a dictionary parameter and Key is the name of the sorted field with a value of 1 or −1.

For dataset example_data_2, place the “age” field in reverse order in Robo 3T, as shown:

image.png

In Python, however, the sort() method for query results returns an error if it uses MongoDB’s writing method, as shown in the figure below:

image.png

In Python, the sort() method takes two arguments: the first argument is the field name, and the second argument is either -1 or 1. It can run normally, as shown in the figure:

image.png

6.4 query _id

In Robo 3T, documents can be queried based on the value of _id. The query statement is as follows:

image.png

When PyMongo is installed, Python automatically installs a third-party library called “bson”. The ObjectId class needs to be imported from the Bson library using the following command:

from bson import ObjectId
collection.find({'_id': ObjectId('5e8ac5dfdd9cf99b7a446e99')})

Copy the code

The running results are as follows:

image.png

summary

This paper first introduces the installation of MongoDB, and then introduces the graphical operation software Of MongoDB, Robo 3T. You can add, delete, modify, and query the MongoDB database by entering commands in the command input window of Robo 3T.

Most of MongoDB’s operations can be smoothly ported to Python. Therefore, in most cases, you can simply copy the MongoDB operation statements from Robo 3T into Python. Of course, there are a few exceptions.

Public account: Operation and maintenance development story

Making:Github.com/orgs/sunsha…

Love life, love operation

If you think the article is good, please click on the upper right corner and select send to friends or forward to moments. Your support and encouragement is my biggest motivation. Please follow me if you like

Scan qr code

Pay attention to me, not regular maintenance of quality content

Warm prompt

If you like this article, please share it in moments, and for more information, please follow me.

.

This article uses the article synchronization assistant to synchronize