MongoDB权威指南 笔记

第2章 MongoDB基础知识1 文档2 集合22 命名5 MongoDB shell简介51 运行shell52 MongoDB客户端53 shell种的基本操作6 数据类型61 基本数据类型62 日期63 数组64 内嵌文档65 _id和ObjectId7 使用MongoDB shell74 定制shell提示第3章 创建更新和删除文档1插入并保存文档11 批
摘要由CSDN通过智能技术生成

第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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值