MongoDB学习笔记:文档Crud Shell
MongoDB学习笔记:文档Crud Shell
文档插入
一、插入语法
db.collection.insertOne() 将单个文档插入到集合中。
db.collection.insertMany() 将多个文件插入集合中。
文档删除
一、删除语法
db.collection.deleteOne() 即使多个文档可以匹配指定的过滤器,也最多删除匹配指定过滤器的单个文档。
db.collection.deleteMany() 删除匹配指定过滤器的所有文档。
文档查询
一、查询语法
db.collection.find( <query filter>, <projection> )
db.collection.findOne() //仅仅返回单个文档,相当于使用limit
<query filter> 查询的过滤条件
<projection> 投影,即哪些列需要返回
对于查询的结果可以添加limits, skips, sort 等方式控制返回的结果集
缺省情况下,在mongo shell中对于未使用将结果集返回给变量的情形下,仅返回前20条记录
注:本文描述中有些地方使用到了文档的键值对,称为键和值,有些地方称为列,是一个概念。
二、准备数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
//演示环境 db.version() 3.2.9 //插入演示数据 db.users.insertMany( [ { _id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: { artist: "Picasso", food: "pizza" }, finished: [ 17, 3 ], badges: [ "blue", "black" ], points: [ { points: 85, bonus: 20 }, { points: 85, bonus: 10 } ] }, { _id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: { artist: "Miro", food: "meringue" }, finished: [ 11, 25 ], badges: [ "green" ], points: [ { points: 85, bonus: 20 }, { points: 64, bonus: 12 } ] }, { _id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: { artist: "Cassatt", food: "cake" }, finished: [ 6 ], badges: [ "blue", "red" ], points: [ { points: 81, bonus: 8 }, { points: 55, bonus: 20 } ] }, { _id: 4, name: "xi", age: 34, //Author : Leshami type: 2, //Blog : http://blog.csdn.net/leshami status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "red", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] }, { _id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: { artist: "Noguchi", food: "nougat" }, finished: [ 14, 6 ], badges: [ "orange" ], points: [ { points: 71, bonus: 20 } ] }, { _id: 6, name: "abc", age: 43, type: 1, status: "A", favorites: { food: "pizza", artist: "Picasso" }, finished: [ 18, 12 ], badges: [ "black", "blue" ], points: [ { points: 78, bonus: 8 }, { points: 57, bonus: 7 } ] } ] ) |
三、演示查询
1.简单查询
|
1
2
3
4
5
6
7
|
//查询所有文档,文档太多,此处及以下演示查询结果省略 db.users.find( {} ) //与方式等价于db.users.find() db.users.findOne( {} ) //查询单条记录 //等值查询,{ <field1: <value1, ... } db.users.find({_id:5}).pretty() db.users.find({age:19,status:"P"}) //多条件等值查询,隐式使用$and运算符 |
2.基于运算符的查询
更多运算符:https://docs.mongodb.com/manual/reference/operator/query/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//基于运算符的查询,{ <field1: { <operator1: <value1 }, ... } //基于$in运算符 db.users.find( { status: { $in: [ "P", "D" ] } } ) //基于$and运算符的查询 db.users.find( {$and: [{ status: "A", age: { $lt: 30 } } ]}) db.users.find( { status: "A", age: { $lt: 30 } } ) //此查询方法与上一条等价,隐式使用$and运算符 //基于$or运算符的查询 db.users.find( { $or: [ { status: "A" }, { age: { $lt: 30 } }]}) //多条件组合查询,基于$lt,也有$or,还有隐式$and db.users.find( { status: "A", $or: [ { age: { $lt: 30 } }, { type: 1 } ] } ) |
3、内嵌文档查询
|
1
2
3
4
5
|
//等值匹配内嵌文档 db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } ) //等值匹配内嵌文档的特定键值,通过"键.成员名:值"的方式来进行 db.users.find( { "favorites.artist": "Picasso" } ) |
4、数组查询
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//查询数组元素 //查询数组badges中包含black的文档 db.users.find( { badges: "black" } ) //匹配一个特定的数组元素 //查询数组badges中第一个元素为black的文档 db.users.find( { "badges.0": "black" } ) //此处0表示数组的下标 //匹配单个数组元素满足条件 //查询数组finished至少有一个元素的值大于15且小于20的文档 db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } ) //匹配混合数组元素满足条件 //查询数组finished中任意的一个元素大于15,且另外一个元素小于20 db.users.find( { finished: { $gt: 15, $lt: 20 } } ) //或者这个元素既大于15又小于20的文档 //查询数组内嵌文档 //查询数组points元素1内嵌文档键points的值小于等于55的文档 db.users.find( { 'points.0.points': { $lte: 55 } } ) //查询数组内嵌文档 //查询数组points内嵌文档键points的值小于等于55的文档,此处未指定数组下标 db.users.find( { 'points.points': { $lte: 55 } } ) //查询数组元素至少一个内嵌文档满足所有条件的文档 //如下,数组points内至少一个文档points键的值小于等于70,bonus键的值等于20的记录,这样的文档被返回 db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } ) //查询数组元素任意一个内嵌文档满足所有条件的文档 //如下,数组points内嵌文档任意一个文档points的值小于等于70,且数组内另外一个文档bonus值等于20 //或者数组内某个内嵌文档points的值小于等于70,bonus的值等于20,这2种情形会被返回 db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } ) |
四、限制查询返回的结果集
|
1
2
3
|
{ field1: <value>, field2: <value> ... } 1 or true 显示该字段 0 or false 不显示该字段 |
1、限制返回的列
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//查询结果中显示字段name及status,缺省情况下,文档的_id列会被返回 > db.users.find( { status: "A" }, { name: 1, status: 1 } ) { "_id" : 2, "name" : "bob", "status" : "A" } { "_id" : 3, "name" : "ahn", "status" : "A" } { "_id" : 6, "name" : "abc", "status" : "A" } //查询结果中显示字段name及status,且不显示_id列 > db.users.find( { status: "A" }, { name: 1, status: 1, _id: 0 } ) { "name" : "bob", "status" : "A" } { "name" : "ahn", "status" : "A" } { "name" : "abc", "status" : "A" } //返回查询中未列出的全部列名 > db.users.find( { status: "A" }, { favorites: 0, points: 0 ,badges:0}) { "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "finished" : [ 11, 25 ] } { "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "finished" : [ 6 ] } { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "finished" : [ 18, 12 ] } //返回内嵌文档指定的列名,相反地,如果不显示内嵌文档的某个列,将在置0即可 > db.users.find( { status: "A" }, { name: 1, status: 1, "favorites.food": 1 } ) { "_id" : 2, "name" : "bob", "status" : "A", "favorites" : { "food" : "meringue" } } { "_id" : 3, "name" : "ahn", "status" : "A", "favorites" : { "food" : "cake" } } { "_id" : 6, "name" : "abc", "status" : "A", "favorites" : { "food" : "pizza" } } //返回数组内内嵌文档的指定列,如下查询为数组points内嵌文档bonus列 > db.users.find( { status: "A" },{ name: 1,"points.bonus": 1 } ) { "_id" : 2, "name" : "bob", "points" : [ { "bonus" : 20 }, { "bonus" : 12 } ] } { "_id" : 3, "name" : "ahn", "points" : [ { "bonus" : 8 }, { "bonus" : 20 } ] } { "_id" : 6, "name" : "abc", "points" : [ { "bonus" : 8 }, { "bonus" : 7 } ] } //下面的查询使用了$slice操作符,这将仅仅返回符合status为A,且显示数组中的最后一个元素 > db.users.find( { status: "A" }, { name: 1, status: 1, points: { $slice: -1 } } ) { "_id" : 2, "name" : "bob", "status" : "A", "points" : [ { "points" : 64, "bonus" : 12 } ] } { "_id" : 3, "name" : "ahn", "status" : "A", "points" : [ { "points" : 55, "bonus" : 20 } ] } { "_id" : 6, "name" : "abc", "status" : "A", "points" : [ { "points" : 57, "bonus" : 7 } ] } |
2、查询NULL值或不存在的键
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//插入文档 > db.users.insert( [ { "_id" : 900, "name" : null }, { "_id" : 901 }, { "_id" : 902,"name" : "Leshami" ,"blog" : "http://blog.csdn.net/leshami"} ] ) //查询name自动为null的文档,注,以下查询中,不存在name列的文档_id:901的也被返回 > db.users.find( { name: null } ) { "_id" : 900, "name" : null } { "_id" : 901 } //通过$type方式返回name为null的文档,此时_id:901未返回 > db.users.find( { name : { $type: 10 } } ) { "_id" : 900, "name" : null } //通过$exists返回name自动不存在的文档 > db.users.find( { name : { $exists: false } } ) { "_id" : 901 } |
五、查询小结
1、文档查询db.users.find()等价于db.users.find( {} )
2、基于and运算符的多个组合条件可以省略and运算符的多个组合条件可以省略and,直接将条件组合即可
3、对于$and运算符内的条件,用[]括起来,相当于数组形式
4、对于数组查询,可以使用基于下标的方式精确配置特定的元素值
5、对于内嵌文档,可以使用”文档键.内嵌文档键”方式进行访问
6、对于数组内内嵌文档的方式,可以使用”数组名.下标.内嵌文档键”方式访问
7、对于哪些列名需要显示可以通过{ field1: <0|1>, … }来设定
8、本文参考:https://docs.mongodb.com/manual/tutorial/query-documents/
文档更新
MongoDB学习笔记:文档Crud Shell的更多相关文章
- MongoDB学习笔记——文档操作之查询
查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...
- MongoDB学习笔记——文档操作之增删改
插入文档 使用db.COLLECTION_NAME.insert() 或 db.COLLECTION_NAME.save() 方法向集合中插入文档 db.users.insert( { user_id ...
- 4. svg学习笔记-文档结构元素和样式的使用
svg除了绘图元素之外还有一部分是专门用于文档结构的,这类元素有<g>,<use>,<defs>,<symbol>等 <g>元素 如果我们仅 ...
- MongoDB 学习笔记之 基本CRUD
Mongo 命令: show databases/dbs; use test; show tables/collections; db.help() db.createCollection('user ...
- StyleCop学习笔记-文档规则
文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...
- winform学习笔记-文档路径
获取应用程序路径 //获取当前进程的完整路径,包含文件名(进程名).string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\x ...
- Linux 编程学习笔记----文档管理系统
本文从网络上完成的第 Linux在文件系统管理. 1.VFS文件系统概述 linux採用VFS来管理文件系统,并且linux设计的原则之中的一个就是everything is file.因此文件管理系 ...
- Spring3.0学习笔记文档的官方网站(六)--3.4.1
3.4 依靠 3.4.1 依赖注入 依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI 参数是引进一个对象的.和缺乏父母之前-子类关系: ...
- MongoDB 学习笔记(一):安装及简单shell操作
一.说明 1.该系列MongoDB学习笔记的学习环境采用的MongoDB版本为mongodb-win32-i386-2.4.6,操作系统为win7. 二.安装 1.新建两个目录,分别是D:\Insta ...
随机推荐
- 前端缓存API请求数据
1. 背景 在一些项目中,有时候会出现不同模块重复请求大量相同api接口的情况,特别是在一些功能相似的后台管理页面中.以下面这几个页面为例,每次进入页面都需要请求等大量重复的下拉框数据,下拉框数据短时 ...
- 幻读:听说有人认为我是被MVCC干掉的
@ 目录 前言 系列文章 一.我是谁? 二.为什么有人会认为我是被MVCC干掉的 三.我真的是被MVCC解决的? 四.再聊当前读.快照读 当前读 快照读 五.告诉你们吧!当前读的情况下我是被next- ...
- 经典论文系列| 实例分割中的新范式-SOLO
前言: 这是实例分割中的一篇经典论文,以往的实例分割模型都比较复杂,这篇论文提出了一个简单且直接的实例分割模型,如何设计这种简单直接的模型且要达到一定的精度往往会存在一些困难,论文中有很多思路或思想值 ...
- Nginx篇
1 基本操作命令 先CD到nginx.exe目录 启动nginx服务 nginx start nginx 优雅停止nginx,有连接时会等连接请求完成再杀死worker进程 nginx -s quit ...
- Unity基础-脚本生命周期
理解Unity脚本的生命周期对游戏开发很重要,这篇文章对生命周期做一个记录和总结.Unity的脚本生命周期(消息),也就是在脚本运行时,自动并且按顺序执行的一系列函数.在unity官网中有对生命周期详 ...
- POJ3228二分最大流
题意: 有n个点,每个点有两个权值,金子数量还有仓库容量,金子可以存在自己的仓库里或者是别的仓库里,仓库和仓库之间有距离,问所有金子都必须存到库里最大距离的最小是多少? 思路: ...
- 绕过网站WAF(图片绕过)
当我们在渗透一个网站的时候,很多时候,会遇到下面这种情况.网站装有WAF,把我们的SQL注入语句给拦截了. 这就是网站的安全狗 此时,我们的渗透会陷入僵局.到底应该如何才能让我们的语句绕过安全狗的检查 ...
- mongo中常用的命令
命令使用mongo shell 执行 1.mongo中增加新字段 mongo shell 进入后执行use table选中要添加字段的库 db.getCollection('表名').update({ ...
- Centos7下搭建gitbook环境踩坑记录
1.安装npm yum -y install npm 2.配置npm仓 npm config set registry https://mirrors.tencent.com/npm/ 3.安装git ...
- SpringCloud-OAuth2(一):基础篇
关于Oauth2 的详细介绍官网地址:https://developer.okta.com/blog/2017/06/21/what-the-heck-is-oauth 1:什么是OAuth2 首先, ...