MongoDB 简介

Python 中的 MongoDB 入门

Filip Schouwenaars

Machine Learning Researcher

数据无处不在!

数据无处不在.jpg

Python 中的 MongoDB 入门

示例:学生课程数据

student_id name course_code title
1 Amina CS101 编程入门
1 Amina MATH201 代数基础
2 Luca CS101 编程入门
Python 中的 MongoDB 入门

如何存储数据

学生

student_id name
1 Amina
2 Luca
Python 中的 MongoDB 入门

如何存储数据

学生

student_id name
1 Amina
2 Luca

课程

course_code title
CS101 编程入门
MATH201 代数基础
Python 中的 MongoDB 入门

如何存储数据

学生

student_id name
1 Amina
2 Luca

选课

student_id course_code
1 CS101
1 MATH201
2 CS101

课程

course_code title
CS101 编程入门
MATH201 代数基础

关系模型

  • 由关系型数据库使用
  • 示例:MySQL、PostgreSQL
Python 中的 MongoDB 入门

当关系型数据库力有不逮时

 

  • 适合结构化且一致的数据
  • 复杂、多变的数据怎么办?
  • 需更多表来存放额外细节
  • 规模增长后更难管理

schematic

Python 中的 MongoDB 入门

走进 MongoDB

 

  • 面向文档,而非表
  • 灵活:每条记录可不同
  • 无需严格模式
  • 轻松存储嵌套的结构化数据

Python 中的 MongoDB 入门

BSON:一种特殊的 JSON

 

  • BSON = 二进制 JSON
  • 相似,但更快、更高效
  • 额外类型,如 Date、Binary
  • MongoDB 底层使用 BSON

Python 中的 MongoDB 入门

连接 MongoDB

from pymongo import MongoClient

# Create client (localhost) client = MongoClient()
# Create client (remote server) client2 = MongoClient("mongodb+srv://usr:[email protected]")
Python 中的 MongoDB 入门

Vamos praticar!

Python 中的 MongoDB 入门

Preparing Video For Download...