In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Python how to operate MongoDB, the article is very detailed, has a certain reference value, interested friends must read it!
1. Preparatory work
Before you begin, make sure that MongoDB is installed and its services are started, and that Python's PyMongo library is installed.
two。 Connect MongoDB
When connecting to MongoDB, we need to use MongoClient in the PyMongo library. Generally speaking, you can pass the IP and port of MongoDB, where the first parameter is address host and the second parameter is port port (if you do not pass it the parameter, the default is 27017):
Import pymongoclient = pymongo.MongoClient (host='localhost', port=27017)
This allows you to create a connection object for MongoDB.
In addition, the first parameter of MongoClient, host, can also be passed directly into the connection string of MongoDB, which starts with mongodb, for example:
Client = MongoClient ('mongodb://localhost:27017/')
This can also achieve the same connection effect.
3. Specify database
Multiple databases can be set up in MongoDB, and then we need to specify which database to operate on. Here we take the test database as an example. The next step is to specify the database to be used in the program:
Db = client.test
Here, call the test property of client to return the test database. Of course, we can also specify:
Db = client ['test']
These two ways are equivalent.
4. Specify a collection
Each database of MongoDB also contains many collections (collection), which are similar to tables in a relational database.
The next step is to specify the collection to operate on, where you specify a collection name of students. Similar to specifying a database, there are two ways to specify a collection:
Collection = db.studentscollection = db ['students']
So we declare a Collection object.
5. Insert data
Next, you can insert the data. For the students collection, create a new piece of student data, which is expressed in a dictionary:
Student = {'id':' 20170101, 'name':' Jordan', 'age': 20,' gender': 'male'}
The student's student number, name, age and gender are specified here. Next, you can insert the data directly by calling the insert () method of collection, as follows:
Result = collection.insert (student) print (result)
In MongoDB, each piece of data is actually uniquely identified by a _ id attribute. If this attribute is not explicitly specified, MongoDB automatically generates a _ id attribute of type ObjectId. The insert () method returns a value of _ id after execution.
The running results are as follows:
5932a68615c2606814c91f3d
Of course, we can also insert multiple pieces of data at the same time, just passing it 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 ([student1, student2]) print (result)
The returned result is a collection of corresponding _ id:
[ObjectId ('5932a80115c2606a59e8a048'), ObjectId (' 5932a80115c2606a59e8a049')]
In fact, the insert () method is no longer officially recommended in PyMongo 3.x. Of course, there is no problem with continuing to use it. It is officially recommended to use the insert_one () and insert_many () methods to insert a single record and multiple records, respectively. Examples are as follows:
Student = {'id':' 20170101, 'name':' Jordan', 'age': 20,' gender': 'male'} result = collection.insert_one (student) print (result) print (result.inserted_id)
The running results are as follows:
5932ab0f15c2606f0c1cf6c5
Unlike the insert () method, the InsertOneResult object is returned this time, and we can call its inserted_id property to get _ id.
For the insert_many () method, we can pass the data as a list, as shown in the following example:
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)
The running results are as follows:
[ObjectId ('5932abf415c2607083d3b2ac'), ObjectId (' 5932abf415c2607083d3b2ad')]
The type returned by this method is InsertManyResult, and you can get the _ id list of the inserted data by calling the inserted_ids property.
6. Query
After inserting the data, we can query using the find_one () or find () methods, where the find_one () query gets a single result and find () returns a generator object. Examples are as follows:
Result = collection.find_one ({'name':' Mike'}) print (type (result)) print (result)
Here, we query the data whose name is Mike, and the returned result is a dictionary. The running result is as follows:
{'_ id': ObjectId ('5932a80115c2606a59e8a049'),' id': '20170202,' name': 'Mike',' age': 21, 'gender':' male'}
As you can see, it adds the _ id attribute, which MongoDB automatically adds during insertion.
In addition, we can also query according to ObjectId. In this case, we need to use objectid in the bson library:
From bson.objectid import ObjectIdresult = collection.find_one ({'_ id': ObjectId ('593278c115c2602667ec6bae')}) print (result)
The query result is still a dictionary type, 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.
For queries with multiple pieces of data, we can use the find () method. For example, look for data with the age of 20 here. The example is as follows:
Results = collection.find ({'age': 20}) print (results) for result in results: print (result)
The running results are as follows:
{'_ id': ObjectId ('593278c115c2602667ec6bae'),' id': '20170101,' name': 'Jordan',' age': 20, 'gender':' male'} {'_ id': ObjectId ('593278c815c2602678bb2b8d'),' id': '20170102,' name': 'Kevin',' age': 20, 'gender':' male'} {'_ id': ObjectId ('593278d815c2609d7645a8'),' id': '20170103' 'name': 'Harden',' age': 20, 'gender':' male'}
The returned result is of type Cursor, which is equivalent to a generator, and we need to traverse all the results, each of which is a dictionary type.
If you want to query data older than 20, write as follows:
Results = collection.find ({'age': {' $gt': 20}})
The conditional key value of the query here is no longer a simple number, but a dictionary whose key name is the comparison symbol $gt, which means greater than, and the key value is 20.
Here, the comparison symbols are summarized in the following table.
Example of symbolic meaning $lt is less than {'age': {' $lt': 20}} $gt is greater than {'age': {' $gt': 20}} $lte less than {'age': {' $lte': 20}} $gte is greater than or equal to {'age': {' $gte': 20}} $ne is not equal to {'age': {' $ne': 20}} $in in range {'age': {' $in': [20] 23]} $nin is not in the range {'age': {' $nin': [20,23]}}
In addition, regular matching queries can be made. For example, to query student data whose name begins with M, an example is as follows:
Results = collection.find ({'name': {' $regex':'^ M.Article'}})
Here you use $regex to specify a regular match, and ^ M.* represents a regular expression that starts with M.
Here, some functional symbols are reclassified into the following table.
Example of symbolic meaning sample meaning $regex matches regular expression {'name': {' $regex':'^ M.Article'} name starts with M whether the $exists attribute exists {'name': {' $exists': True}} name attribute exists $type type judgment {'age': {' $type': 'int'}} age is int$mod digital mode operation {' age': {'$mod': [5 0]}} age module 5 more than 0$ text text query {'$text': {'$search': 'Mike'}} text type attribute contains Mike string $where advanced condition query {' $where': 'obj.fans_count = = obj.follows_count'} the number of fans equals the number of followers 7. Count
To count how many pieces of data there are in the query result, you can call the count () method. For example, count all data items:
Count = collection.find () .count () print (count)
Or count the data that meet certain conditions:
Count = collection.find ({'age': 20}) .count () print (count)
The result of the run is a numeric value, that is, the number of pieces of data that meet the criteria.
8. Sort
When sorting, call the sort () method directly, and pass in the sorted field and the ascending and descending order flag. Examples are as follows:
Results = collection.find () .sort ('name', pymongo.ASCENDING) print ([result [' name'] for result in results])
The running results are as follows:
['Harden',' Jordan', 'Kevin',' Mark', 'Mike']
Here we call pymongo.ASCENDING to specify ascending order. If you want to sort in descending order, you can pass in pymongo.DESCENDING.
9. Offset
In some cases, we may want to take only a few elements, and then we can use the skip () method to offset a few positions, such as offset 2, to ignore the first two elements and get the third and later elements:
Results = collection.find () .sort ('name', pymongo.ASCENDING) .skip (2) print ([result [' name'] for result in results])
The running results are as follows:
['Kevin',' Mark', 'Mike']
In addition, you can specify the number of results to fetch with the limit () method, as shown in the following example:
Results = collection.find (). Sort ('name', pymongo.ASCENDING) .skip (2) .limit (2) print ([result [' name'] for result in results])
The running results are as follows:
['Kevin',' Mark']
Without the limit () method, three results would have been returned, but with restrictions, two results would have been intercepted.
It is worth noting that when the number of databases is very large, such as tens of millions, it is best not to use large offsets to query data, because this is likely to lead to memory overflow. At this point, you can use operations similar to the following to query:
From bson.objectid import ObjectIdcollection.find ({'_ id': {'$gt': ObjectId ('593278c815c2602678bb2b8d')}})
At this point, you need to record the _ id of the last query.
10. Update
For data updates, we can use the update () method to specify the conditions for the update 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 want to update the age of the data whose name is Kevin: first specify the query condition, and then query the data. After modifying the age, call the update () method to pass in the original condition and the modified data.
The running results are as follows:
{'ok': 1,' nModified': 1, 'updatedExisting': True: 1,' updatedExisting': True}
The returned result is in the form of a dictionary, ok represents successful execution, and nModified represents the number of data items affected.
Alternatively, we can update the data using the $set operator, as follows:
Result = collection.update (condition, {'$set': student})
This allows you to update only the fields that exist in the student dictionary. If there are other fields before, they will not be updated or deleted. If you don't use $set, all the previous data will be replaced with a student dictionary; if other fields exist, they will be deleted.
In addition, the update () method is also officially deprecated. It is also divided into update_one () method and update_many () method, which are used more strictly, and their second parameter needs to use the $type operator as the key name of the dictionary. The example is as follows:
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, and the second parameter can no longer be passed directly into the modified dictionary, but needs to be in the form {'$set': student}, which returns a UpdateResult type. Then call the matched_count and modified_count properties respectively to get the number of matched and affected pieces of data.
The running results are as follows:
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, the query condition is specified as age greater than 20, and then the update condition is {'$inc': {'age': 1}}, that is, age plus 1. After execution, the age of the first eligible data will be added by 1.
The running results are as follows:
1 1
You can see that the number of matches is 1, and the number of influence is 1.
If you call the update_many () method, all eligible data will be updated, as shown in the following example:
Condition = {'age': {' $gt': 20}} result = collection.update_many (condition, {'$inc': {'age': 1}}) print (result) print (result.matched_count, result.modified_count)
At this point, the number of matching entries is no longer 1, and the running result is as follows:
3 3
As you can see, all the matching data will be updated.
11. Delete
The deletion operation is relatively simple. You can directly call the remove () method to specify the deletion conditions, and all data that meets the criteria will be deleted. Examples are as follows:
Result = collection.remove ({'name':' Kevin'}) print (result)
The running results are as follows:
{'ok': 1,' nasty: 1}
In addition, there are still two new recommended methods-delete_one () and delete_many (). Examples are as follows:
Result = collection.delete_one ({'name':' Kevin'}) print (result) print (result.deleted_count) result = collection.delete_many ({'age': {' $lt': 25}}) print (result.deleted_count)
The running results are as follows:
fourteen
Delete_one () deletes the first eligible data, and delete_many () deletes all eligible data. Their return results are of type DeleteResult, and you can call the deleted_count property to get the number of data items deleted.
twelve。 Other actions
In addition, PyMongo provides some combined methods, such as find_one_and_delete (), find_one_and_replace (), and find_one_and_update (), which are delete, replace, and update operations after finding, and their usage is basically the same as above.
In addition, you can also operate on the index, and the related methods are create_index (), create_indexes (), drop_index (), and so on.
The above is all the contents of the article "how Python operates MongoDB". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.