https://blog.csdn.net/bicheng4769/article/details/79579830

项目背景

最近在项目中使用mongdb来保存压测结果中的监控数据,那么在获取监控数据时,遇到这样一个问题: 一个doucument中包含一个内嵌数组,其中内嵌数组也是分成好几类的数组(可以通过标识判断),那么我只需要返回特定的数组,而不是返回内嵌数组的所有数据。 
原始数据:

{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"addTime" : ISODate("2018-03-16T03:05:04.363Z"),
"tag" : "test",
"userInfo" : [
{
"name" : "cj",
"address" : "江苏",
"age" : 24.0,
"userTag" : "stu"
},
{
"name" : "hj",
"address" : "江苏",
"age" : 26.0,
"userTag" : "stu"
},
{
"name" : "lbl",
"address" : "美国",
"age" : 22.0,
"userTag" : "teach"
}
]
}
  • 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

查询条件是 tag =“test” userTag=”teach” 的学生的信息。期望返回的结果如下所示:

{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"userInfo" : [
{
"name" : "lbl",
"address" : "美国",
"age" : 22.0,
"userTag" : "teach"
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

但大多是find 的结果 是这样的

db.test.find({"userInfo.userTag":"teach","tag":"test"})
{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"addTime" : ISODate("2018-03-16T03:05:04.363Z"),
"tag" : "test",
"userInfo" : [
{
"name" : "cj",
"address" : "江苏",
"age" : 24.0,
"userTag" : "stu"
},
{
"name" : "hj",
"address" : "江苏",
"age" : 26.0,
"userTag" : "stu"
},
{
"name" : "lbl",
"address" : "美国",
"age" : 22.0,
"userTag" : "teach"
}
]
}
  • 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

$elemMatch 介绍

其实我们在学习一个新的东西的时候,我建议是去官方文档查看一下,毕竟官方的才是最权威的。官方地址: 
https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch
我们可以看到 $elemMatch 是在projections方法下面,projections代表是限制返回字段的方法。 
修改后的方法:

db.test.find({"userInfo.userTag":"teach","tag":"test"},{"userInfo":{$elemMatch{"userTag":"teach"}}})
  • 1

执行结果

{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"userInfo" : [
{
"name" : "lbl",
"address" : "美国",
"age" : 22.0,
"userTag" : "teach"
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

终于得到我想要的结果了 hhhhhhhhhhhhhhhhhhhhh。 
然后我又想获取userInfo.userTag = “stu” 的数据,很简单啊

db.test.find({"userInfo.userTag":"teach","tag":"test"},{"userInfo":{$elemMatch:{"userTag":"stu"}}})
  • 1

但是结果出人意料:

{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"userInfo" : [
{
"name" : "cj",
"address" : "江苏",
"age" : 24.0,
"userTag" : "stu"
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

明明是2条stu的结果,为什么至返回一条呢? 其实$elemMatch 的定义 在官网中已经说过了,这是原话:

The $elemMatch operator limits the contents of an <array> field from the query results to contain only the first element matching the $elemMatch condition.
  • 1

注意 only the first element 也就是仅仅匹配第一个合适的元素。 
那么 对于数组中只有一个返回元素,我们可以使用$elemMatch来查询,但是对于多个元素$elemMatch 是不适应。

$Aggregation介绍

文档地址:https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

  1. $unwind: 
    https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/#pipe._S_unwind 
    大概意思就是将数组中的每一个元素转为每一条文档

  2. $match: 
    https://docs.mongodb.com/manual/reference/operator/aggregation/match/ 
    简单的过滤文档,条件查询。

  3. $project 
    https://docs.mongodb.com/manual/reference/operator/aggregation/project/ 
    修改输入文档的结构

执行命令:

db.test.aggregate([{"$unwind":"$userInfo"},
{"$match":{"userInfo.userTag":"stu","tag":"test"}},
{"$project":{"userInfo":1}}])
  • 1
  • 2
  • 3

结果

/* 1 */
{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"userInfo" : {
"name" : "cj",
"address" : "江苏",
"age" : 24.0,
"userTag" : "stu"
}
} /* 2 */
{
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
"userInfo" : {
"name" : "hj",
"address" : "江苏",
"age" : 26.0,
"userTag" : "stu"
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

这样的一个结果就是我们想要的。感兴趣的同学可以分别执行下这个三个操作(比较看看三个结果有什么不同),你就能理解 $unwind、$match、$project 三个方法的作用

db.test.aggregate([{"$unwind":"$userInfo"}])
db.test.aggregate([{"$unwind":"$userInfo"},{"$match":{"userInfo.userTag":"stu","tag":"test"}}])
db.test.aggregate([{"$unwind":"$userInfo"},{"$match":{"userInfo.userTag":"stu","tag":"test"}},{"$project":{"userInfo":1}}])
  • 1
  • 2
  • 3

总结

  1. 之前查询内嵌数组时,采用的方法是将整条document查询出来之后,在对内嵌数组进行代码过滤。只是觉得这种查询方式并没有用到mongodb的其他的一些方法,还是需求驱动学习。
  2. 学习一个新的东西建议从官方文档开始学习。

结束语:

MongoDB查询内嵌数组(限定返回符合条件的数组中的数据)(1)的更多相关文章

  1. mongodb查询内嵌文档

    mongodb查询内嵌文档   假设有这样一个文档: db.XXX.remove(); db.XXX.insert({"id":1, "members":[{& ...

  2. mongodb 内嵌数组查询问题: 如何限定返回与条件匹配的数组

    原文地址:https://segmentfault.com/q/1010000002943721

  3. mongoskin 是让 Node.js 支持 MongoDB 的内嵌访问层。

    mongoskin 是让 Node.js 支持 MongoDB 的内嵌访问层.

  4. Array 数组filter()方法,遍历数组返回符合条件的一个新数组

    <script> const arr = [ { id: 1, name: "aa", isDone: false }, { id: 2, name: "bb ...

  5. matlab学习笔记12_2创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段

    一起来学matlab-matlab学习笔记12 12_2 结构体 创建结构体数组,访问标量结构体,访问非标量结构体数组的属性,访问嵌套结构体中的数据,访问非标量结构体数组中多个元素的字段 觉得有用的话 ...

  6. thymeleaf的内联th:inline(在javascript访问model中的数据)

    thymeleaf模板引擎为前端数据的获取提供了较大的便利,在html标签内可通过th标签加${}表达式访问model里的对象数据.但如果不想通过th标签而是简单地访问model对象数据,或是想在ja ...

  7. MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))

    目录 一.前言 1. 运行环境 二.前期准备工作 1. 创建 MongoDBContext MongoDb操作上下文类 2.创建测试类 3.创建测试代码 三.内嵌数组增加元素操作 1.Update.S ...

  8. 学习MongoDB 五: MongoDB查询(数组、内嵌文档)(二)

    一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...

  9. MongoDB查询或修改内嵌文档

    作为非关系型数据库中的佼佼者,MongoDB一大优势在于能够在一条文档中存储对象类型的数据,适当增加冗余来让数据库更好用.文档中一个对象类型的字段在MongoDB中被称为内嵌文档(Embedded) ...

随机推荐

  1. Java遍历HashMap并修改(remove)(转载)

    遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操 ...

  2. Day 5-<补充> 类的的继承和查找顺序

    类的继承于查找顺序: 在py2中,不继承object的类为经典类,经典类继承查找:深度优先. 在py3中,默认继承object,所以python3中都是新式类,新式类的继承查找:广度优先. 类的特殊属 ...

  3. Linux 文件及目录管理命令基础

    pwd   显示当前所在目录 cd 切换目录 cd 命令语法 cd [选项] 目录 cd 的常用选项: cd ~ /cd 切换到当前用户的加目录 cd . 保持当前目录不变 cd .. 切换到上级目录 ...

  4. python之路--面向对象-成员

    一 . 成员 在类中你能写的所有内容都是类的成员 class 类名: # 方法 def__init__(self, 参数1, 参数2...): # 属性变量 self.属性1 = 参数1 self.属 ...

  5. Vue之computed计算属性

    demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  6. 扩展运算符(spread)是三个点(…)

    扩展运算符(spread)是三个点(…),将一个数组||类数组||字符串转为用逗号分隔的序列. js中用来对数组进行操作,把数组里面的东西统统拿出来 一.展开数组 //展开数组 let a = [1, ...

  7. Python——Label控件说明

    Anchor :   标签中文本的位置: background(bg)foreground(fg) :背景色:前景色: borderwidth(bd) :边框宽度: width  .height   ...

  8. SQL字段类型bit 查询时注意

    sql 查询时  字段=1 或 字段=0 c# 里也是

  9. How to flash Havoc on enchilada

    update fastboot and adb fastboot oem unlock adb debug enchilada reboot to fastboot fastboot devices ...

  10. 安装mysql zip5.6版--安裝

    第一步当然是下载了,我下载的是压缩包形式的安装包,这种直接解压就可以了,地址是:https://dev.mysql.com/downloads/file/?id=468784 第二步就是解压了,解压到 ...