[MongoDB detailed tutorial] four, python operation MongoDB

The use of third-party libraries to implement python pymongo operation of MongoDB's
pymongo official document: https://api.mongodb.com/python/current/tutorial.html

1, the installation pymongo

pip install 安装pymongo

2, the database connection

import pymongo

client = pymongo.MongoClient('localhost', 27017)    # 连接服务器,需要先开启服务
db = client['mymongo']  # 选择数据库
data = db.students.find()   # 查询数据,返回一个游标,通过对游标进行遍历来获取每条数据 
print(db)
print(data)


# 对查询到的数据进行遍历,每一项为一个dict
for i in data:
    print(i, type(i))

Return result:

MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'mymongo')
<pymongo.cursor.Cursor object at 0x1058e57f0>
{'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年级'} <class 'dict'>
{'_id': ObjectId('5db642bc0f98841018f76966'), 'name': 'wang', 'age': 19.0, 'grade': '二年级'} <class 'dict'>
{'_id': ObjectId('5db653920f98841018f7696b'), 'name': 'xu', 'age': 20.0, 'grade': '三年级', 'text': ['女', '研究员']} <class 'dict'>
{'_id': ObjectId('5db654660f98841018f7696c'), 'name': 'ma', 'age': 20.0, 'grade': '二年级', 'text': ['女', '副教授', '副处长']} <class 'dict'>
{'_id': ObjectId('5db68d190f98841018f76970'), 'name': 'cheng', 'age': 21.0, 'grade': '四年级'} <class 'dict'>
{'_id': ObjectId('5db68f6c0f98841018f76971'), 'name': 'cheng', 'age': 22.0, 'grade': '五年级'} <class 'dict'>

3, operation of the database

python mysql and oracle operation are done using direct sql,
operated by the method of pymongo MongoDB is provided to complete.

This section no longer issue separate grammar, all the "students" are words set name.

3.1, search

data_all = db.students.find()   # 查询全部
data_lim = db.students.find().limit(1)  # 返回第一条
data_the = db.students.find({"name": "xu"})  # 条件查询(结果只有1条匹配)
data_one = db.students.find_one()   # 查询一条


print(data_all, type(data_all))
print(data_lim, type(data_lim))
print(data_the, type(data_the))
print(data_one, type(data_one))

Although the three methods is a data obtained, but only when used returned dict .find_one (), returns the rest are the Cursor (cursor), you may need to be traversed to get specific data.

<pymongo.cursor.Cursor object at 0x105a04780> <class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x105a047f0> <class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x105a04860> <class 'pymongo.cursor.Cursor'>
{'_id': ObjectId('5db642b30f98841018f76965'), 'name': 'chen', 'age': 18.0, 'grade': '一年级'} <class 'dict'>

3.2, by

# 插入单条
db.students.insert_one({"name": "zuo", "age": 40, "grate": "九年级"})

# 插入多条
many_data = [{"name": "ding", "age": 40, "grate": "九年级"},
             {"name": "liao", "age": 42, "grate": "十年级"},
             {"name": "zhao", "age": 35, "grate": "九年级"}]
db.students.insert_many(many_data)

3.3, change

# 修改单条
db.students.update_one(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})

# 修改全部匹配项
db.students.update_many(filter={"name": "zuo"}, update={"$set": {"grate": "十年级"}})


# filter后为条件,update后为修改后值,其中$set为固定语法。

3.4, delete

# 删除单条
db.students.delete_one({})  # 删除全部数据的第一条
db.students.delete_one({"name": "zuo"}) # 删除匹配项的第一条

# 删除多条
db.students.delete_many({"name":"zuo"}) # 删除集合中的全部数据
db.students.delete_many({"name":"zuo"}) # 删除全部匹配项

Guess you like

Origin www.cnblogs.com/cbowen/p/11755480.html