MongoDB 2.5、与python交互

fangxiaoji 2020-01-30

2.5、与python交互

进入虚拟环境
sudo pip install pymongo
或源码安装
python setup.py
  • 引入包pymongo
import pymongo

类MongoClient

  • 连接,创建客户端
无安全认证:client=pymongo.MongoClient(‘mongodb://localhost:27017‘)
有安全认证:client=pymongo.MongoClient(‘mongodb://用户名:密码@localhost:27017/数据库名称‘)

类database

  • 获得数据库test1
db=client.test1

类collection

  • 主要方法如下
  1. insert_one()
  2. insert_many()
  3. update_one()
  4. update_many()
  5. delete_one()
  6. delete_many()
  7. find_one()
  8. find()
  • 获取集合stu
stu = db.stu
  • 添加文档,可以返回文档的id
s1={name:‘gj‘,age:18}
s1_id = stu.insert_one(s1).inserted_idprint(s1_id)
  • 修改文档

scores.update_one({‘name‘:‘zsf‘},{‘$set‘:{‘name‘:‘张三丰‘}})

  • 删除文档

scores.delete_one({‘name‘:‘zsf‘})

  • 查找一个文档,将文档转换为了一个字典返回
ret = stu.find_one()
print(ret)print(ret[‘name‘])ret = stu.find_one({‘name‘:‘张三丰‘})print(ret)print(ret[‘name‘])
  • 查找多个文档1,返回一个Cursor类型的对象,用于遍历,遍历时,每个文档以字典的形式返回
cursor = stu.find()for s in cursor:    print(s)    print(s[‘name‘])cursor = stu.find({‘name‘:‘张三丰‘})for s in cursor:    print(s)    print(s[‘name‘])
  • 查找多个文档2
cur=stu.find()
cur.next()
cur.next()
cur.next()
  • 获取文档个数
print stu.count()
  • 排序,返回cursor类型的对象
  • 升序使用ASCENDING,降序使用DESCENDING:

单属性:cur = stu.find().sort(‘age‘, DESCENDING)

多属性:cur = stu.find().sort([(‘age‘, DESCENDING),(‘name‘, ASCENDING)])

  • 子集
cur=stu.find().skip(2).limit(3)
 
# -*- coding:utf-8 -*-
# @Time : 2020/1/29 23:16
# @Author : xxxx
# @File : test1_mongodb.py
# @Software: PyCharm

import pymongo

def test0():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 添加
    scores.insert_one({‘name‘:‘张一丰‘})

    # 删除
    scores.delete_one({‘name‘:‘张一丰‘})

    # 修改
    scores.update_one({‘name‘:‘zsf‘},{‘$set‘:{‘name‘:‘张三丰‘}})

def test1():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询
    ret = scores.find_one()
    print(ret)
    print(ret[‘name‘])

def test2():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询
    ret = scores.find_one({‘name‘:‘张三丰‘})
    print(ret)
    print(ret[‘name‘])

def test3():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询
    cursor = scores.find()
    for s in cursor:
        print(s)
        print(s[‘name‘])

def test4():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询
    cursor = scores.find({‘name‘:‘张三丰‘})
    for s in cursor:
        print(s)
        print(s[‘name‘])

def test5():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询
    # ret = scores.find({‘age‘:{‘$gt‘:20}})
    # for s in ret:
    #     print(s[‘name‘])

def test6():
    # 获取客户端,建立连接
    cli = pymongo.MongoClient(‘mongodb://localhost:27017‘)
    # 切换数据库
    db = cli.liuyan
    # 获取集合
    scores = db.scores

    # 查询、排序
    # cursor = scores.find().sort(‘name‘, pymongo.DESCENDING)
    cursor = scores.find().sort([(‘name‘, pymongo.DESCENDING), (‘age‘, pymongo.ASCENDING)])
    for s in cursor:
        # print(s)
        print(s[‘name‘])

if __name__ == ‘__main__‘:
    # test0()
    # test1()
    # test2()
    # test3()
    # test4()
    # test5()
    test6()

相关推荐