Pymongo
yespython
An operation ofMongodb
Library of.
Let’s briefly list some common operation commands.
Connect database
The linked database needs to provide an address and interface. First, import the package.
from pymongo import MongoClient
conn = MongoClient('localhost',27017)
Of course, you can use the following wording:
conn = MongoClient('mongodb://localhost:27017/')
Create database
mongodb
You do not need to create a database in advance, but use it directly. If it is not found, it will be created automatically.
db = conn.testdb
The above statement will create atestdb
Database. However, when no data is inserted, the database cannot be seen (not displayed) in the management tool.
insert data
First, let’s insert a piece of data and have a look.
Single record insertion
from pymongo import MongoClient
conn = MongoClient('mongodb://localhost:27017/')
db = conn.testdb
db. Col.insert ({"name": "Yanying", "province": "Jiangsu", "age": 25})
be careful:The core operation in the database will be ignored, and the code in the next operation will be written directly.
python
Nothing happened on the console. That’s what success means. Using the management tool to view the database record does contain a piece of data.
Insert multiple records
Mongodb
You can also insert multiple pieces of data at a time
db.col.insert([
{"name": 'Yanying', 'province': 'Jiangsu', 'age': 25},
{"name": 'Zhang San','province ':' Zhejiang ','age': 24},
{"name": 'Zhang San 1','province ':' Zhejiang 1 ','age': 25},
{"name": 'Zhang San 2', 'province': 'Zhejiang 2', 'age': 26},
{"name": 'Zhang San 3', 'province': 'Zhejiang 3', 'age': 28},
])
Query data
Let’s query the data just inserted.
single query
We can usefind_one()
To query a record.
db.col.find_one()
The above statement can query onemongodb
record. Extra in the record_id
Is the only value automatically generated by mongodb.
{'_id': objectid ('5925351ad92fac3250b9ae3f '),'name':'yanying ',' province ':'jiangsu', 'age': 25}
Let’s insert some more data for the following operations. (tens of thousands of words omitted)
Query all
If we need to query all records, we can usedb.col.find()
But what is found is a result resource set.
We can usefor
To list all records.
for item in db.col.find():
print(item)
In this way, all records can be obtained.
{'_id': objectid ('5925351ad92fac3250b9ae3f '),'name':'yanying ',' province ':'jiangsu', 'age': 25}
{'_id': objectid ('592550e5d92fac0b8c449f87 '),' name ':' Zhangsan ',' province ':' Beijing ',' age ': 29}
{'_id': objectid ('592550f6d92fac3548c20b1a '),' name ':' Lisi ',' province ':' Shanghai ',' age ': 22}
{'_id': objectid ('59255118d92fac43dcb1999a '),' name ':' Wang Erma ',' province ':' Guangdong ',' age ': 30}
Condition query
Just insert the query criteria as parameters to filter the data.
for item in db.col.find({'name':"yanying"}):
print(item)
Query results
{'_id': objectid ('5925351ad92fac3250b9ae3f '),'name':'yanying ',' province ':'jiangsu', 'age': 25}
Of course, you can also query records that are less than a certain value
for item in db.col.find({"age":{"$lt":25}}):
print(item)
Or records greater than a certain value
for item in db.col.find({"age":{"$gt":25}}):
print(item)
Statistical query
The above code can count the number of all records
db.col.find().count() // 4
Or add some conditions
db.col.find({"age":{"$gt":25}}).count() //2
According to_ ID query record
_id
yesmongodb
Automatically generatedid
, whose type isObjectId
, you need to convert the type if you want to use it.
python3
This method is provided in, but you need to import a library.
from bson.objectid import ObjectId
In this way, it can be used directly_id
Make an inquiry.
collection.find_one({'_id':ObjectId('592550e5d92fac0b8c449f87')})
Result sorting
Just put the fields to be sorted intosort
The method is enough,Mongodb
The default is ascending
db.col.find().sort("age")
However, you can also add some parameters to change the sorting method. For example, reverse order, but remember to import firstpymongo
library
import pymongo
db.col.find().sort("UserName",pymongo.DESCENDING)
You can also put him in ascending order, even by default
for item in db.col.find().sort('age',pymongo.ASCENDING):
print(item)
Update data
Updating data is very simple. You only need one condition and the data to be updated
db. Col. update ({'_id': objectid ('59255118d92fac43dcb1999a ')}, {' $set ': {' name ':' Wang Erma 33333 '})
The results are as follows:Wang Erma
BecomeWang Erma 33333
{'_id': objectid ('59255118d92fac43dcb1999a '),' name ':' Wang Erma 33333 ',' province ':' Guangdong ',' age ': 30}
Delete data
Delete data usageremove()
Method. If the method has conditions, delete the specified condition data, otherwise delete all
deletename
It is the user of Wang Erma 33333.
db. Col. remove ({'name': 'Wang Erma 33333'})
Delete all data(Use with caution)
db.col.remove()