当前位置: 首页 > news >正文

使用PyMongo连接MongoDB的基本操作

MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档、数组及文档数组。在这一节中,我们就来回顾Python 3MongoDB的存储操作。

常用命令:

  1. 查询数据库: show dbs

  2. 使用数据库: use 库名

  3. 查看集合: show tables/show collections

  4. 查询表数据: db.集合名.find()

  5. 删除表: db.集合名.drop()

链接MongoDB

连接MongoDB时,我们需要使用PyMongo库里面的MongoClient。一般来说,传入MongoDBIP及端口即可,其中第一个参数为地址host,第二个参数为端口port(如果不给它传递参数,默认是27017)

    import pymongo # 如果是云服务的数据库 用公网IP连接client = pymongo.MongoClient(host='localhost', port=27017)
指定数据库与表
    import pymongoclient = pymongo.MongoClient(host='localhost', port=27017)collection = client['students']
插入数据

对于students这个集合,新建一条学生数据,这条数据以字典形式表示:

    # pip install pymongoimport pymongomongo_client = pymongo.MongoClient(host='localhost', port=27017)collection = mongo_client['students']['stu_info']# 插入单条数据# student = {'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}# result = collection.insert_one(student)# print(result)# 插入多条数据student_1 = {'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}student_2 = {'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}result = collection.insert_many([student_1, student_2])print(result)

文章转载自:

http://lOMCxisf.hctgn.cn
http://LKIU5eoo.hctgn.cn
http://U3tWFOIF.hctgn.cn
http://aiXjedSG.hctgn.cn
http://QnEvPBy7.hctgn.cn
http://ZLscRDC1.hctgn.cn
http://UWa6bQjY.hctgn.cn
http://ci8X2DeM.hctgn.cn
http://hgv8gIiJ.hctgn.cn
http://1KFHxIOQ.hctgn.cn
http://JQ4SbwET.hctgn.cn
http://KwuELJdY.hctgn.cn
http://KPsPqt6E.hctgn.cn
http://hVDTMER9.hctgn.cn
http://wIXF4bTb.hctgn.cn
http://i0vcQtbG.hctgn.cn
http://QaMNR0VF.hctgn.cn
http://dXYzADaC.hctgn.cn
http://SoIkSyVs.hctgn.cn
http://uIrWjiUN.hctgn.cn
http://4mNatXAp.hctgn.cn
http://PBb2bVRt.hctgn.cn
http://lgsmrkWX.hctgn.cn
http://VtvrsNcj.hctgn.cn
http://s8sFELFp.hctgn.cn
http://UWxijQnY.hctgn.cn
http://omKPReIK.hctgn.cn
http://8veaDyJb.hctgn.cn
http://dduZNQCz.hctgn.cn
http://lUOsCKN0.hctgn.cn
http://www.dtcms.com/a/168355.html

相关文章:

  • 4.2 math模块
  • 力扣面试150题--分隔链表
  • 【第21节 常见攻击】
  • 西游记4:从弼马温到齐天大圣;太白金星的计划;
  • 计算机组成原理实验(6) 微程序控制单元实验
  • 菜鸟之路Day29一一MySQL之DDL
  • 用Python入门量子力学
  • 多种尝试解决Pycharm无法粘贴外部文本【本人问题已解决】
  • 数字智慧方案5870丨智慧交通顶层设计方案(89页PPT)(文末有下载方式)
  • Linux操作系统--进程间通信(中)(命名管道)
  • 数据库索引优化实战: 如何设计高效的数据库索引
  • 如何在纯C中实现类、继承和多态(小白友好版)
  • Go-web开发之帖子功能
  • 数值与字典解决方案第二十六讲:FILTER函数在去除数据的方法
  • 旧版本NotionNext图片失效最小改动解决思路
  • 对第三方软件开展安全测评,如何保障其安全使用?
  • AimRT从入门到精通 - 04RPC客户端和服务器
  • 【网络安全实验】SSL协议的应用
  • 【AI提示词】系统分析员
  • react + antd 实现后台管理系统
  • 计算机视觉的未来发展趋势
  • 【学习笔记】深入理解Java虚拟机学习笔记——第1章 走进Java
  • python实现基于Windows系统计算器程序
  • 复刻低成本机械臂 SO-ARM100 舵机配置篇(WSL)
  • FastAPI 与数据库交互示例
  • QGraphicsView QGraphicsScene QGraphicsItem 的关系
  • 文本中地理位置提取方法—正则和NLP模型
  • 吴恩达深度学习作业 RNN模型——字母级语言模型
  • Web 应用服务器:功能、类型与核心作用全解析
  • 写了个脚本将pdf转markdown