pymongo 的使用

1.链接数据库(database)和聚类(collection)

import pymongo

# 连接 MongoDB,默认使用本地链接的 27017 端口
client = pymongo.MongoClient('localhost', 27017)

# 连接到具体某一个数据库
test = client['test_db']

# 也可写成这种形式
test = client.test_db

# 链接到 student_info 这个聚类
student_info = test['student_info']

Mongodb 中的 数据库(database)等同于 SQL 中的 database,可以理解为一个 Excel 文件。

Mongodb 中的 聚类(collection),相当于 SQL 中的 表(table),可以理解为 一个 Excel 文件里面的 sheet。




2.插入数据

使用 insert_one() 语句插入一条数据,数据包装成字典的形式。

 test.student_info.insert_one({'name':'小白'})

用迭代插入多条数据:

student_infos = [
    {'name':'小明','age':12,'city':'广州','hobby':['足球','游戏']},
    {'name':'小李','age':11,'city':'北京','hobby':['篮球']},
    {'name':'小张','age':9,'city':'上海','hobby':['吃饭','睡觉','发呆']},
    {'name':'小丽','age':12,'city':'苏州','hobby':['音乐']},
    {'name':'小红','age':8,'city':'南京','hobby':['读书']}
    ]

for student in student_infos:
    student_info.insert_one(
        {
            'name':student['name'],
            'city':student['city'],
            'age':student['age'],
            'hobby':student['hobby']
            }
        )

insert_many() 语句插入多条记录:

test.student_info.\
    insert_many(
        [
            {'name':'小x', 'age': 10},
            {'name':'小y', 'age': 11},
            {'name':'小z', 'age': 12}
        ]
        )




3.查询操作

3.1 查看全部聚集名称

>>> test.collection_names()
['student_info']

3.2 查看聚集的第一条记录

>>> test.student_info.find_one()
{
    '_id': ObjectId('5a1cd216f1a19d178cc7f6c8'), 
    'city': '广州', 
    'name': '小明', 
    'age': 12, 
    'hobby': ['足球', '游戏']
}

3.3 条件查询

# 查找 age 为 12 的记录
r = test.student_info.\
    find_one(
        {'age':12}
        )
print(r)

# 结果
{
    'age': 12, 
    'hobby': ['足球', '游戏'], 
    'city': '广州', 
    '_id': ObjectId('5a1cd216f1a19d178cc7f6c8'), 
    'name': '小明'
}

注意:符合搜索条件的记录是有多条的,find_one() 语句只会显示其中一条。查询全部合符条件的记录,使用 find() 语句。

r = test.student_info.\
    find(
        {'age':12}
        )

3.4 查看聚集的记录统计

# 查看聚集的总数
>>> test.student_info.find().count()
5
# 查看聚类中某个键的所有值
>>> test.student_info.distinct('city')
['广州', '北京', '上海', '苏州', '南京']

3.5 聚集查询结果排序

# 默认为升序
r = test.student_info.\
    find().\
    sort("age")
# 升序
r = test.student_info.\
    find().\
    sort("age", pymongo.ASCENDING)
# 降序
r = test.student_info.\
    find().\
    sort("age", pymongo.DESCENDING)
# 多重排序
# 先根据 age 排序,age 相等时再按 name 排序
r = test.student_info.\
    find().\
    sort(
        [
            ("age",pymongo.ASCENDING),
            ("name",pymongo.DESCENDING)
        ]
        )




4.修改操作

4.1 修改一条记录

# 找到 age = 12 的记录
# 然后修改其中一条的 age 为 100
test.student_info.\
    update_one(
        {'age': 12},
        {
        '$set':{'age': 100}
        }
    )

4.2 修改多条记录

test.student_info.\
    update_many(
        {'age': 100},
        {
        '$set':{'age': 1}
        }
    )

4.3 修改全部记录

# 以_id为索引,遍历全部记录,并把每一条的'age'值修改为:100
for i in test.student_info.find():
    test.student_info.\
    update_one(
        {'_id':i['_id']}, 
        {'$set': 
            {'age': 100}
        }
        )




5.替换操作

替换一条记录:

# 找出 city 值为 '北京 '的记录,替换为 'hometown':'廊坊'
# 注意:是替换记录内的全部内容,并非只是替换 'city' 字段
test.student_info.replace_one(
    {'city':'北京'},
    {'hometown':'廊坊'}
    )

r = test.student_info.find({'hometown':'廊坊'})
for i in r:
    print(i)

# 结果
{
    '_id': ObjectId('571c901b13d5942564a5a23e'), 
    'hometown': '廊坊'
}




6.删除操作

6.1删除一条记录

# 删除第一条 name  为 '小明' 的记录
test.student_info.delete_one({'name':'小明'})

6.2 删除多条记录

# 删除全部 age 为 12 的记录
test.student_info.delete_many({'age':12})

你可能感兴趣的:(pymongo 的使用)