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的更多相关文章

  1. MongoDB学习笔记——文档操作之查询

    查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...

  2. MongoDB学习笔记——文档操作之增删改

    插入文档 使用db.COLLECTION_NAME.insert() 或 db.COLLECTION_NAME.save() 方法向集合中插入文档 db.users.insert( { user_id ...

  3. 4. svg学习笔记-文档结构元素和样式的使用

    svg除了绘图元素之外还有一部分是专门用于文档结构的,这类元素有<g>,<use>,<defs>,<symbol>等 <g>元素 如果我们仅 ...

  4. MongoDB 学习笔记之 基本CRUD

    Mongo 命令: show databases/dbs; use test; show tables/collections; db.help() db.createCollection('user ...

  5. StyleCop学习笔记-文档规则

    文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...

  6. winform学习笔记-文档路径

    获取应用程序路径 //获取当前进程的完整路径,包含文件名(进程名).string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\x ...

  7. Linux 编程学习笔记----文档管理系统

    本文从网络上完成的第 Linux在文件系统管理. 1.VFS文件系统概述 linux採用VFS来管理文件系统,并且linux设计的原则之中的一个就是everything is file.因此文件管理系 ...

  8. Spring3.0学习笔记文档的官方网站(六)--3.4.1

    3.4 依靠 3.4.1 依赖注入     依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI     参数是引进一个对象的.和缺乏父母之前-子类关系: ...

  9. MongoDB 学习笔记(一):安装及简单shell操作

    一.说明 1.该系列MongoDB学习笔记的学习环境采用的MongoDB版本为mongodb-win32-i386-2.4.6,操作系统为win7. 二.安装 1.新建两个目录,分别是D:\Insta ...

随机推荐

  1. SpringBoot自动装配,比较全的吧,来看看吧~

    文章挺长,表达不好,希望能有获~~~~~~~ Spring也提供使用注解来注册bean,为什么要用SpringBoot呢? 使用Spring应用,比如SpringMVC还行需要配置ViewResolv ...

  2. aws EKS

    登陆aws账号 1)找到eks 相关的项目,并进入 2)填写集群的名称,然后下一步 3)集群设置页面,添加集群服务角色 (aws eks cluster role) 4)继续集群配置 5)集群创建完成 ...

  3. Git 简介与仓库使用

    1. Git 简介 2. 远程仓库的使用 3. 本地仓库的使用 1. Git 简介 Git 是分布式版本控制系统,同一个 Git 仓库,可以分布到不同的机器上. 其原理是首先找一台电脑充当服务器的角色 ...

  4. 794. Valid Tic-Tac-Toe State

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  5. 从苏宁电器到卡巴斯基第23篇:难忘的三年硕士时光 I

    初次接触逆向工程 不知不觉就来到了2013年的9月份,学校开学了,我开始正式体验研究生的生活了.按道理来说,硕士研究生是需要围绕在导师身边,每天朝九晚五地去实验室做项目的.不过我们老师没有项目,也不要 ...

  6. Http Get与Post的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  7. 线程操作与进程挂起(Windows核心编程)

    0x01 线程挂起与切换 对于挂起进程,挂起线程则比较简单,利用 ResumeThread 与 SuspendThread 即可,当线挂起时线程用户状态下的一切操作都会暂停 #include < ...

  8. [CTF]思维导向图

    [CTF]思维导向图 ---------------来自大佬的CTF思维导向图 Angel_Kitty https://www.cnblogs.com/ECJTUACM-873284962/ 给信息安 ...

  9. C#/VB.NET 自定义动画路径

    PPT中的动画效果可分为已有内置动画以及自定义动画.设置内置动画,只需直接指定动画效果类型即可.本文主要介绍如何实现自定义动画,即自定义形状动作线性路径.附C#及VB.NET代码供参考. 程序运行环境 ...

  10. druid-spring-boot-starter的配置

    #数据源基本信息 spring: datasource: druid: username: root password: 123456 url: jdbc:mysql://localhost:3306 ...