SoFunction
Updated on 2024-11-12

Analyzing common operations of the Python pymongo module

This article example describes the Python pymongo module common operations. Shared for your reference, as follows:

matrix:pymongo3.0.3,python3

Here are some of the things I've put together to do with pymongo, many of which are available online using the()Go to connect to the database, but my connection here has been prompted by the absence of this package, if you have any solutions or other need to add, also welcome to tell me.

First, import pymongo, use MongClient to connect to the database, connect to the myinfo database

import pymongo
client= ("127.0.0.1",27017)
db=

Two,insertinsert_one()Only one piece of data can be inserted, the format for inserting multiple pieces of data is([{Article 1}, {Article 2}])If you add [], you must add [], otherwise you will only add the first entry (user is a collection, you can use db["collection"] in addition toto operate on the collection

db["user"].insert_one({"name":"zhao"})
db["user"].insert_one({"name":"zhou","age":"5"})
db["user"].insert([{"name":"wu","age":"6"},{"name":"zheng","age":"7"}])

*insert can also be inserted in the following way to separate the data

data = [
    {"name":"zhao","rank":"1"},
    {"name":"qian","rank":"2"},
    {"name":"sun","rank":"3"},
    {"name":"li","rank":"4"},
    ]
(data)

Third, update, $ set: update operation, multi = True: whether the query to all the data to operate, upsert = True: if you can not find the results of the query whether to insert a data

.update_one({"age":"2"},{"$set":{"name":"qian","age":2}})
({"name":"sun"},{"$set":{"name":"qian"}},upsert=True)

*update_onealso can only operate on a piece of data, $ set is the update operation of the $ operator, you can also use $ inc or $ push, the first two operations are about the same speed, $ push operation is slower.

Fourth, remove, if the latter () does not fill in the content, is to empty the entire table..find_one_and_delete()It also means delete.

({"name":"wu"})
.find_one_and_delete({"name":"zheng"})

Five,()If you don't fill in anything in (), you are counting all the data in the set.

print(({"age":"6"}))

VI. Print out the search results

from bson import json_util as jsonb
print((list(({"name":"wu"}))))
print(({"name":"wu"}))

You can see the comparison of the above two ways, without conversion and after conversion as follows:

*()converts the results of the query into a readable list format, otherwise the printout would be< object at 0x02096DF0>this format

(math.) ergodiccol1=()All the results of the query, and its key=name value

for i in col1:
  print(i)
  print(i["name"])

Readers interested in more Python related content can check out this site's topic: theSummary of common database manipulation techniques in Python》、《Summary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.