The PyMongo method in Python3 is described in detail.

Source: Internet
Author: User
Tags mongoclient

The PyMongo method in Python3 is described in detail.

Preface

This article mainly introduces how to use PyMongo in Python3. I will share it with you for your reference. I will not talk about it here. Let's take a look at the details:

MongoDB Storage

Here, let's take a look at the MongoDB storage operations under Python3. Before starting this section, make sure that you have installed MongoDB and started its services. In addition, you have installed Python's PyMongo library.

If you are not installed, refer to this article.

Connect to MongoDB

To connect to MongoDB, we need to use the MongoClient in the PyMongo library. Generally, we can pass in the IP address and port of MongoDB. The first parameter is the address host, and the second parameter is the port, if this parameter is not set, the default value is 27017.

import pymongoclient = pymongo.MongoClient(host='localhost', port=27017)

In this way, we can create a connection object for MongoDB.

In addition, the first parameter host of the MongoClient can directly transmit the connection string of MongoDB, starting with mongodb. For example:

client = MongoClient('mongodb://localhost:27017/')

The same connection effect can be achieved.

Specified database

MongoDB is also divided into databases. The next step is to specify which database to operate. Here, we will take the test database as an example. Therefore, we need to specify the database to be used in the program in the next step.

db = client.test

You can call the test attribute of the client to return to the test database. Of course, you can also specify the value as follows:

db = client['test']

The two methods are equivalent.

Set

Each MongoDB database contains many collections, similar to tables in relational databases. In the next step, we need to specify the set to be operated. Here we specify a set named students, student collections. Similar to a specified database, a specified set can also be used in two ways.

collection = db.students
collection = db['students']

Insert data

Next, we can insert data. For the students Collection, we create a new student data, which is represented in a dictionary:

student = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}

Here, we specify the student's student ID, name, age, and gender, and then directly call the collectioninsert()Method To insert data.

result = collection.insert(student)print(result)

In MongoDB, each piece of data actually has a unique _ id attribute. If _ id is not explicitly specified, MongoDB will automatically generate an ObjectId type _ id attribute.insert()The _ id value returned after the method is executed.

Running result:

5932a68615c2606814c91f3d

Of course, we can also insert multiple data records at the same time, which only needs to be passed in the form of a list, for example:

student1 = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}student2 = { 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}result = collection.insert([student1, student2])print(result)

The returned result is a set of corresponding _ IDs. The running result is as follows:

[ObjectId('5932a80115c2606a59e8a048'), ObjectId('5932a80115c2606a59e8a049')]

In Python 3. X,insert()The method is not officially recommended. Of course, it is no problem to continue using it. It is officially recommended.insert_one()Andinsert_many()Method to separate one entry and multiple records.

student = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}result = collection.insert_one(student)print(result)print(result.inserted_id)

Running result:

<pymongo.results.InsertOneResult object at 0x10d68b558>5932ab0f15c2606f0c1cf6c5

Returned results andinsert()Different methods. The returned result is the InsertOneResult object. We can call its inserted_id attribute to obtain the _ id.

Forinsert_many()Method, we can pass the data in the form of a list, the example is as follows:

student1 = { 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}student2 = { 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}result = collection.insert_many([student1, student2])print(result)print(result.inserted_ids)

insert_many()The type returned by the method is InsertManyResult. You can call the inserted_ids attribute to obtain the _ id list of inserted data. The running result is as follows:

<pymongo.results.InsertManyResult object at 0x101dea558>[ObjectId('5932abf415c2607083d3b2ac'), ObjectId('5932abf415c2607083d3b2ad')]

Query

After inserting data, we can usefind_one()Orfind()Method to query,find_one()The query result is a single result,find()Multiple results are returned.

result = collection.find_one({'name': 'Mike'})print(type(result))print(result)

Here we query the data whose name is Mike. The returned result is of the dictionary type and the running result is as follows:

<class 'dict'>{'_id': ObjectId('5932a80115c2606a59e8a049'), 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}

It can be found that it has an additional _ id attribute, which is automatically added during MongoDB insertion.

We can also directly query Objects Based on ObjectId. here we need to use the ObjectId in the bson library.

from bson.objectid import ObjectIdresult = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})print(result)

The query result is still of the dictionary type. The running result is as follows:

{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}

Of course, if the query result does not exist, None is returned.

You can usefind()Method, for example, to search for data of the age of 20, the example is as follows:

results = collection.find({'age': 20})print(results)for result in results: print(result)

Running result:

<pymongo.cursor.Cursor object at 0x1032d5128>{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}{'_id': ObjectId('593278c815c2602678bb2b8d'), 'id': '20170102', 'name': 'Kevin', 'age': 20, 'gender': 'male'}{'_id': ObjectId('593278d815c260269d7645a8'), 'id': '20170103', 'name': 'Harden', 'age': 20, 'gender': 'male'}

The returned result is of the Cursor type, which is equivalent to a generator. We need to traverse and retrieve all the results. Each result is of the dictionary type.

To query data older than 20, write the following statement:

results = collection.find({'age': {'$gt': 20}})

The condition key value queried here is not simply a number, but a dictionary. Its key name is a comparison symbol $ gt, which means that the value is greater than and the key value is 20, in this way, you can query all data older than 20.

The comparison symbols are summarized as follows:

Symbol Description Example
$ Lt Less {'Age': {'$ lt': 20 }}
$ Gt Greater {'Age': {'$ gt': 20 }}
$ Lte Less than or equal {'Age': {'$ lte': 20 }}
$ Gte Greater than or equal {'Age': {'$ gte': 20 }}
$ Ne Not equal {'Age': {'$ ne': 20 }}
$ In In scope {'Age': {'$ in': [20, 23]}
$ Nin Out of Scope {'Age': {'$ nin': [20, 23]}

In addition, you can perform regular expression matching queries. For example, you can query student data whose names start with M, for example:

results = collection.find({'name': {'$regex': '^M.*'}})

$ Regex is used to specify Regular Expression matching. ^ M. * indicates the regular expression starting with M, so that you can query all results that match this regular expression.

Here, some function symbols are further classified as follows:

Symbol Description Example Meaning
$ Regex Match Regular Expressions {'Name': {'$ regex': '^ M .*'}} Name starts with M
$ Exists Whether the property exists {'Name': {'$ exists': True }} Name attribute exists
$ Type Type Determination {'Age': {'$ type': 'int '}} The age type is int.
$ Mod Digital mode operation {'Age': {'$ mod': [5, 0]} Age model 5 + 0
$ Text Text Query {'$ Text': {' $ search': 'Mike '}} The text type attribute contains the Mike string.
$ Where Advanced condition Query {'$ Where': 'obj. fans_count = obj. follows_count '} The number of followers equals to the number of followers.

More detailed usage of these operations can be found in the MongoDB official documentation: https://docs.mongodb.com/manual/reference/operator/query/

Count

You can callcount()Method, such as counting the number of all data entries:

count = collection.find().count()print(count)

You can also collect data that meets certain conditions:

count = collection.find({'age': 20}).count()print(count)

Sort

You can call the sort method to pass in the sorting field and the ascending/descending flag. The example is as follows:

results = collection.find().sort('name', pymongo.ASCENDING)print([result['name'] for result in results])

Running result:

['Harden', 'Jordan', 'Kevin', 'Mark', 'Mike']

Offset

In some cases, we may want to take only a few elements.skip()Method offset, for example, offset 2. Ignore the first two elements and obtain the third and later elements.

results = collection.find().sort('name', pymongo.ASCENDING).skip(2)print([result['name'] for result in results])

Running result:

['Kevin', 'Mark', 'Mike']

You can also uselimit()Method to specify the number of results to be retrieved, for example:

results = collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)print([result['name'] for result in results])

Running result:

['Kevin', 'Mark']

If you do not addlimit()Three results will be returned. After the restriction is added, two results will be intercepted and returned.

It is worth noting that when the number of databases is very large, such as tens of millions or hundreds of millions, it is best not to use a large offset to query data, it is likely to cause memory overflow, you can use a similarfind({'_id': {'$gt': ObjectId('593278c815c2602678bb2b8d')}}) This method records the _ id of the last query.

Update

You can useupdate()Method to specify the updated conditions and the updated data. For example:

condition = {'name': 'Kevin'}student = collection.find_one(condition)student['age'] = 25result = collection.update(condition, student)print(result)

Here, we will update the age of data whose name is Kevin. First, we will specify the query conditions, then we will query the data and modify the age, then, call the update method to pass in the original condition and modified data to complete data update.

Running result:

{'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

The returned results are in a dictionary. OK indicates that the execution is successful, and nModified indicates the number of affected data entries.

In additionupdate()This method is not officially recommended.update_one()Method andupdate_many()The usage is more strict. The second parameter must use the $ operator as the dictionary key name. Let's take a look at the example.

condition = {'name': 'Kevin'}student = collection.find_one(condition)student['age'] = 26result = collection.update_one(condition, {'$set': student})print(result)print(result.matched_count, result.modified_count)

The update_one method is called here. The second parameter cannot be passed into the modified dictionary directly, but must be used.{'$set': student}In this form, the returned result is of the UpdateResult type. Then, you can call the matched_count and modified_count attributes to obtain the number of matched data records and the number of affected data records.

Running result:

<pymongo.results.UpdateResult object at 0x10d17b678>1 0

Let's look at another example:

condition = {'age': {'$gt': 20}}result = collection.update_one(condition, {'$inc': {'age': 1}})print(result)print(result.matched_count, result.modified_count)

Here, we specify the query condition as age greater than 20, and then update the condition{'$inc': {'age': 1}} , That is, add 1 to the age. After the execution, the age of the first qualified data plus 1 is displayed.

Running result:

<pymongo.results.UpdateResult object at 0x10b8874c8>1 1

You can see that the number of matching entries is 1, and the number of affected entries is also 1.

If you callupdate_many()Method to update all the data that meets the conditions, for example:

condition = {'age': {'$gt': 20}}result = collection.update_many(condition, {'$inc': {'age': 1}})print(result)print(result.matched_count, result.modified_count)

In this case, the number of matching entries is no longer one. The running result is as follows:

<pymongo.results.UpdateResult object at 0x10c6384c8>3 3

We can see that all matched data will be updated.

Delete

The delete operation is simple and can be called directly.remove()The method specifies the deletion condition. All data that meets the condition will be deleted, as shown in the following example:

result = collection.remove({'name': 'Kevin'})print(result)

Running result:

{'ok': 1, 'n': 1}

There are still two new recommendation methods,delete_one()Anddelete_many()Example:

result = collection.delete_one({'name': 'Kevin'})print(result)print(result.deleted_count)result = collection.delete_many({'age': {'$lt': 25}})print(result.deleted_count)

Running result:

<pymongo.results.DeleteResult object at 0x10e6ba4c8>14

delete_one()That is, to delete the first qualified data,delete_many()That is, to delete all data that meets the conditions, the returned result is of the DeleteResult type. You can call the deleted_count attribute to obtain the number of deleted data records.

More

In addition, PyMongo also provides some combination methods, suchfind_one_and_delete(),find_one_and_replace() ,find_one_and_update()Is to delete, replace, and update operations after searching. The usage is basically the same as that described above.

You can also perform operations on indexes, suchcreate_index(),create_indexes() ,drop_index().

For detailed usage, see the official documentation: http://api.mongodb.com/python/current/api/pymongo/collection.html

In addition to the database, set itself and some other operations, here are not one to explain, you can refer to the official documentation: http://api.mongodb.com/python/current/api/pymongo/

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.