MongoDB权威指南 笔记
来源:互联网 发布:安卓一键清除软件数据 编辑:程序博客网 时间:2024/04/29 02:46
- 第2章 MongoDB基础知识
- 1 文档
- 2 集合
- 22 命名
- 5 MongoDB shell简介
- 51 运行shell
- 52 MongoDB客户端
- 53 shell种的基本操作
- 6 数据类型
- 61 基本数据类型
- 62 日期
- 63 数组
- 64 内嵌文档
- 65 _id和ObjectId
- 7 使用MongoDB shell
- 74 定制shell提示
- 第3章 创建更新和删除文档
- 1插入并保存文档
- 11 批量插入
- 12 插入校验
- 2删除文档
- 3 更新文档
- 31 文档替换
- 32 使用修改器
- set 修改器入门
- inc 增加或减少
- 数组修改器
- 添加元素
- 将数组作为数据集使用
- 删除元素
- 33 upsert 特殊的更新
- 34 更新多个文档
- 35 返回被更新的文档
- 写入安全机制
- 1插入并保存文档
第2章 MongoDB基础知识
2.1 文档
- MongoDB区分大小写,文档不能有相同的key
- \0是结束的标志,不能作为集合名或者key的名
2.2 集合
2.2.2 命名
- .和$是保留字
- 子集合,用 . 分隔
2.5 MongoDB shell简介
2.5.1 运行shell
2.5.2 MongoDB客户端
- db 查看所用数据库
- show dbs 查看数据库
- use db 使用某个数据库
- show collections 查看集合
2.5.3 shell种的基本操作
insert 创建
post = {"title": "My Blog Post","content": "Here's my blog post","date": new Date()}db.blog.insert(post);
update 更新
post = {"title": "My Blog Post","content": "Here's my blog post","date": new Date()} post.comments = [];db.blog.update({title: "My Blog Post"}, post);
remove 删除
db.blog.remove(query) //没有query会全部删除
2.6 数据类型
2.6.1 基本数据类型
- null
null 用于表示空值或者不存在的字段js{"x" : null}
- boolean
{"x" : true}
- number
The shell defaults to using 64-bit floating point numbers. Thus, these numbers look
“normal” in the shell:
{"x" : 3.14}//or:{"x" : 3}
For integers, use the NumberInt or NumberLong classes, which represent 4-byte or
8-byte signed integers, respectively.
{"x" : NumberInt("3")}{"x" : NumberLong("3")}
- string
Any string of UTF-8 characters can be represented using the string type:{"x" : "foobar"}
- date
Dates are stored as milliseconds since the epoch. The time zone is not stored:{"x" : new Date()}
- regular expression
Queries can use regular expressions using JavaScript’s regular expression syntax:{"x" : /foobar/i}
- array
Sets or lists of values can be represented as arrays:{"x" : ["a", "b", "c"]}
- embedded document 内嵌文档
Documents can contain entire documents embedded as values in a parent
document:{"x" : {"foo" : "bar"}}
Data Types | 17
Download from Wow! eBook
2.6.2 日期
2.6.3 数组
查询的时候,可以查询数组里的某个元素
存在users:[‘1261651’,’321321354’]
例如query={users: ‘15915154578’}
2.6.4 内嵌文档
2.6.5 _id和ObjectId
- _id 在集合中必须唯一,是ObjectId对象
- 创建文档没有_id键的时候系统会帮你创建一个
2.7 使用MongoDB shell
2.7.4 定制shell提示
第3章 创建、更新和删除文档
3.1插入并保存文档
> db.foo.insert({bar:"barz"})
3.1.1 批量插入
- batchInsert
> db.foo.batchInsert([{},{},{}])
- continueOnError 选项
3.1.2 插入校验
- 16M 当前文档最大值
- 容易插入非法值
3.2删除文档
- remove(query)
- 不能撤销,不能恢复
//插入100万条数据for(var i = 0; i < 1000000;i++){ db.rms.insert({foo:'bar',users:[],create_time:new Date()});}//删除var timeRemoves = function() { var start = (new Date()).getTime(); db.rms.remove(); db.findOne(); // makes sure the remove finishes before continuing var timeDiff = (new Date()).getTime() - start; print("Remove took: "+timeDiff+"ms"); }> timeRemoves()
3.3 更新文档
update至少要2个参数,一个定位,一个是修改器modifier,用于说明要对找到的文档进行哪些修改。
3.3.1 文档替换
db.col.update(query,newdata)
3.3.2 使用修改器
部分文档需要更新的时候。
更新修改器 update modifier 是种特殊的键。
1.”$set” 修改器入门
- “$set”用来指定一个字段的值。如果这个字段不存在,则创建它。
db.col.update(query,{"$set": {"key":"value"})
- 也可以可以更新字段的类型,例如把String字段改成数组。
db.col.update(query,{"$set": {"key":["",""]}})
- 用
"$unset"
将键完全删除:db.col.update(query,{"$set":{"key":1}})
- 用”$set”修改内嵌文档
db.col.update(query,{"$set":{"key.subkey":"value"})
2.”$inc” 增加或减少
给一个人加50分,如果不存在score键会创建 db.games.findOne(query,{"$inc": {"score": 50}})
3. 数组修改器
4. 添加元素
用”
db.man.update(query,{"$push": {"books":{ "$each":["书名1","书名2","书名3"]}}})
- 保证数组不会超出设定的最大长度。
db.device.update(query,{"$push":{ "users": { "$each": ["lance","alice"] ,"$slice": -10 }}})
以上例子,保证了一个设备最多10个最新添加的用户
5.将数组作为数据集使用
保证数组内的元素不会重复,使用 “
例如,如果作者不在引文列表中,就添加进去
db.papers.update({"authors cited": {"$ne":"Richie"}}, {"$push": {"authors cited": "Richie"}})
$addToSet 也可以实现 db.papers.update(query,{"$addToSet": {"emails": "654789123@qq.com"}})
如果要添加多个
db.papers.update(query,{"$addToSet": {"emails":{"$each":["654789123@qq.com","123456789@qq.com"]}}})
6.删除元素
3.3.3 upsert 特殊的更新
3.3.4 更新多个文档
3.3.5 返回被更新的文档
写入安全机制
- mongodb权威指南笔记
- MongoDB权威指南笔记
- MongoDB权威指南 笔记
- MongoDB权威指南笔记 2 Mongodb入门
- 学习《Mongodb权威指南》学习笔记 (一)
- 学习《Mongodb权威指南》学习笔记 (二)
- MongoDB权威指南--阅读笔记(一)
- MongoDB权威指南--阅读笔记(二)
- MongoDB权威指南--阅读笔记(三)
- 【mongodb】mongodb 权威指南笔记之增删改查
- MongoDB权威指南
- 《mongodb权威指南》学习
- MongoDB权威指南-简介
- MongoDB权威指南
- mongodb权威指南
- mongodb权威指南-基础
- MongoDB权威指南学习笔记(1)—简介
- MongoDB权威指南学习笔记(2)—入门
- docker命令详解
- 代码中设置TextView的drawableTop
- CTS 问题 第四篇
- 微信小程序开发
- 在MFC中使用CListCtrl,实现每行的行高不同
- MongoDB权威指南 笔记
- <转>Spark中组件Mllib的学习26之逻辑回归-简单数据集,带预测
- The Knowledge of Linux-.-Day04
- 如何在wine下为Source Insight配置字体
- Java并发编程的艺术(五)——中断
- (HDU 5738) <计算几何 ,在n个点中找所有共线的集合> 2016 Multi-University Training Contest 2
- Java 基础知识积累 时间Date那点事
- 简单的android Listview使用例子
- java求整数的位数