MongoDB 2.5、与python交互

2.5、与python交互

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

类MongoClient

  • 连接,创建客户端
无安全认证:client=pymongo.MongoClient(
)
有安全认证:client=pymongo.MongoClient(
) 

类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_id
print(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()
复制代码

 

posted @   LiuYanYGZ  阅读(221)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示