SlideShare a Scribd company logo
1 of 22
Download to read offline
初识 MongoDB
张光宇 2015/11/22
基本概念
文档 Document
◦ MongoDB中的一条数据记录
◦ 格式为BSON (Binary JSON)
集合 Collection
◦ 文档的集合,同一集合中的文档通常格式相似(不要求)
◦ 对应于关系型数据库的数据表
数据库 Database
◦ 数据的仓库,相当于数据的隔离空间,不同仓库之间不通。
◦ 包含一个或多个文档
JSON
JSON = JavaScript Object Notation
格式:
{
"name1": 1,
"name2": "hello",
"name3": {
"a": 1,
"b": 2,
"c": "hello"
},
"name4": ["a", "b", "c"]
}
BSON
磁盘中的存储格式
◦ JSON是字符串,BSON是二进制格式
BSON的特点
◦ 第一个字段一定是_id,表示文档的id(集合内唯一)
◦ 最重要的额外数据类型:
◦ ObjectId 全局唯一的12字节ID号
◦ ISODate 表示日期时间
mongo shell
mongo shell是MongoDB的命令行客户端程序
基于V8引擎的JavaScript语法,支持ECMAScript5
终端输入mongo启动mongo shell
数据库操作
查看当前数据库:db
选择数据库:use <db name>
列出所有数据库:show dbs
删除当前数据库:db.dropDatabase()
数据库不需要显式创建
集合操作
设集合名字为 your_coll
db.your_coll 是表示集合的对象
列出所有集合:show collections
删除集合:db.your_coll.drop()
集合也不需要显式创建
显式创建集合:db.createCollection(<name>,<options>)
文档操作
插入文档:db.your_coll.insert(<document>)
查询文档:
◦ 列出所有文档:db.your_coll.find()
◦ 过滤某个字段:db.your_coll.find({"field": value})
更新文档:
◦ db.your_coll.update({query}, {update_operation}, {options})
删除文档:
◦ db.your_coll.remove({query})
查询文档
列出所有文档:db.your_coll.find()
过滤某个字段:db.your_coll.find({field:value})
过滤条件:
◦ 最大值,最小值:{field: {$gt: 5, $lt: 9}}
◦ 范围:{field: {$in: ["hello", "world"]}}
◦ AND: {field: value, field2: value2}
◦ OR: {$or: [{field: value}, {field2: value2}]}
◦ 子文档查询:{'field.subfield': value}
返回匹配的第一个文档db.your_coll.findOne({query})
指针操作
指针由find函数返回
Cursor.sort({field1:1,field2: -1})
◦ 按field1升序,再按field2降序
Cursor.skip(n)
◦ 跳过前n条数据
Cursor.limit(n)
◦ 限制Cursor数据集大小为n
Cursor.next()
◦ 返回下一条数据
Cursor.hasNext()
◦ 有没有下一条
更新文档
更新文档:db.your_coll.update({query},{update_operations},{options})
query同find参数
更新操作:
◦ $set 设置值
◦ $inc 原子加减法
◦ $rename 修改字段名
◦ $unset 删除字段
◦ $push 向数组添加元素
◦ $pop 删除第一个或最后一个元素
选项:
◦ multi: bool 只更新第一个查询结果还是更新所有的(默认false)
数据库设计对比
通讯录
要存储的信息:
◦ 姓名
◦ 通讯地址
◦ 电话号码(多个)
文档结构
{
"_id": ObjectId(…),
"name": "name",
"address": "somewhere",
"phone_number": [ 18712345678, 18743218765 ]
}
通讯录的SQL版
或者
user_id name address
user_id phone_number
user_id name address phone_number
博客文章和评论
{
_id: 文章ID
user_id: 用户ID
user: {
avatar: 头像
name: 名字
}
tags: [ 标签字符串列表 ]
title: 文章标题
content: 文章内容
comments: [
{
user_id: 评论用户ID
user: {avatar , name}
content: 评论内容
},...]
}
或者,只存user_id
昵称和头像从缓存获取
博客文章和评论SQL版
blahblah
id user_id title content
id blog_id tag
id blog_id user_id content
MongoDB性能好?
MongoDB单层文档和SQL数据单个表理论上性能相近
利用MongoDB内嵌文档可避免部分JOIN操作
MongoDB不支持事务,虽然可以模拟事务
注意:MongoDB单文档大小限制为16MB
GridFS
GridFS
MongoDB每个文档限制为16MB
GridFS专门用于存大于16MB的文件,将文件分块后存入多个文档
文件存储于两个集合内:
◦ fs.files 文件信息
◦ fs.chunks 文件数据
fs.files 格式
_id: 文件ID
chunkSize:块大小,默认255KB,可配置
uploadDate:上传时间
length:长度
filename:文件名(可重复)
fs.chunks
_id: 块ID
files_id:所属文件的ID
n: 文件的第几块
data:数据
mongofiles命令
列出所有文件:mongofiles list
搜索部分文件名:mongofiles search
下载文件:mongofiles get / mongofiles get_id
上传文件:mongofiles put
删除文件:mongofiles delete / mongofiles delete_id

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Mongo基础