Home > Backend Development > Python Tutorial > 9 steps to operate mongodb with Python

9 steps to operate mongodb with Python

不言
Release: 2018-06-04 11:49:51
Original
1475 people have browsed it

This article shares with you the detailed steps and example codes for operating mongodb in Python. Friends in need can refer to it.

1 Import pymongo

from pymongo import MongoClient
Copy after login

2 Connect to the server port number 27017

Connect to MongoDB

To connect to MongoDB we need to use the MongoClient in the PyMongo library. Generally speaking, just pass in the IP and port of MongoDB. The first parameter is the address host, and the second parameter is the port. If the port is not passed, the default is 27017.

conn = MongoClient("localhost")
MongoClient(host='127.0.0.1',port=27017)
Copy after login

Three connection database

db = conn.Database name

Connection collection

collection = db[collection_name]
or
collection = db.collection_name
Copy after login

View all collection names

db.collection_names()
Copy after login

Four Insert Data
(1) Insert one piece of data

db.user.insert({"name":"夏利刚","age":18,"hobby":"学习"})
Copy after login

(2) Insert multiple pieces of data

db.user.insert([{"name":"夏利刚","age":18,"hobby":"学习"},{"name":"xxxoo","age":48,"hobby":"学习"}]
Copy after login

(3) It is recommended to use

insert_one 插入一条数据
insert_many() 插入多条数据
Copy after login
## on 3.x and above #(4) Return id and use insert_one()

data.inserted_id
data.inserted_ids
Copy after login

五query data

(1) Query all

db.user.find()

#带条件的查询
# data = db.user.find({"name":"周日"})
# print(data) #返回result类似一个迭代器 可以使用 next方法 一个一个 的取出来
# print(next(data)) #取出一条数据
Copy after login

2) Query a

db.user.find_one()
Copy after login

(3) Query with conditions

db.user.find({"name":"张三"})
Copy after login

(4) Query id

from bson.objectid import ObjectId*#用于ID查询
data = db.user.find({"_id":ObjectId("59a2d304b961661b209f8da1")})
Copy after login

(5) Fuzzy query

(1){"name":{'$regex':"张"}}
(2)import re {'xxx':re.compile('xxx')}
Copy after login

Six sort limit count skip

(1) sort sort

age is greater than 10

data = db.user.find({"age":{"$gt":10}}).sort("age",-1) #年龄 升序 查询 pymongo.ASCENDING --升序
data = db.user.find({"age":{"$gt":10}}).sort("age",1) #年龄 降序 查询 pymongo.DESCENDING --降序
Copy after login

(2) limit value

take three pieces of data

db.user.find().limit(3)
data = db.user.find({"age":{"$gt":10}}).sort("age",-1).limit(3)
Copy after login

(3) count The number of statistical data items

db.user.find().count()

(4) skip From which piece of data to start

db.user.find ().skip(2)

seven update modification

The update() method is actually a method that is not officially recommended. Here it is also divided into update_one() method and update_many() method. The usage is more strict,

(1) update()

db.user.update({"name":"张三"},{"$set":{"age":25}})
db.user.update({"name":"张三"},{"$inc":{"age":25}})
Copy after login

(2) update_one() Update the first qualifying data

db.user.update_one({"name":"张三"},{"$set":{"age":99}})
Copy after login

(3) update_many() Update all qualifying data

db.user.update_many({"name":"张三"},{"$set":{"age":91}})
Copy after login

(4) Its return result It is an UpdateResult type, and then call the matched_count and modified_count attributes to get the number of matching data and the number of affected data respectively.

print(result.matched_count, result.modified_count) No

eight remove Delete


The deletion operation is relatively simple. Directly call the remove() method to specify the conditions for deletion. That’s it, all data that meets the conditions will be deleted,

(1) Delete Zhang San

collection.remove({"name":"lilei"})
Copy after login
collection.remove({"name":"lilei"})

(2) Delete all

collection.remove()
Copy after login

(3) There are still two new recommended methods, delete_one() and delete_many() methods. The examples are as follows:

delete_one()即删除第一条符合条件的数据
collection.delete_one({“name”:“ Kevin”})
delete_many()即删除所有符合条件的数据,返回结果是DeleteResult类型
collection.delete_many({“age”: {$lt:25}})
Copy after login

(4) You can call the deleted_count attribute to get the deleted data strips number.

result.deleted_count
Copy after login

Nine close connections

conn.close()
Copy after login
Related recommendations:

How to operate MongoDB with PHP and simple analysis

The above is the detailed content of 9 steps to operate mongodb with Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template