原文链接:http://blog.csdn.net/renfufei/article/details/78320176

MongoDB中根据数组子元素进行匹配,有两种方式。

  • 使用 “[数组名].[子元素字段名]” 的方式进行匹配。
  • 使用 “[数组名]” $elemMatch { [子元素字段名] }的方式。

不同点在于所匹配的主体不同。

“[数组名].[子元素字段名]” 的方式匹配的主体为 “[数组名]”, 适用于单个条件,如果是多个条件, 则变成数组子元素之间的“或”运算。

请看示例:

假设某个集合内有2条数据:

document1 如下:

{
"_id" : "123",
"name" : "人文医学",
"qList" : [
{
"qid" : 1,
"content" : "医学伦理学的公正原则",
"reorderFlag" : 1
},
{
"qid" : 2,
"content" : "制定有关人体实验的基本原则",
"reorderFlag" : 0
}
]
}

document2如下:

{
"_id" : "124",
"name" : "人文医学2",
"qList" : [
{
"qid" : 1,
"content" : "医学伦理学的公正原则",
"reorderFlag" : 0
},
{
"qid" : 2,
"content" : "制定有关人体实验的基本原则",
"reorderFlag" : 1
}
]
}

找出数组中, 具有 qid=1并且reorderFlag=0的记录

查询数组内同一条记录同时满足2个条件的语句:

{ "qList": { $elemMatch: { "qid": 1, "reorderFlag": 0} } }

查询结果是:

{
"_id" : "124",
"name" : "人文医学2",
"qList" : [
{
"qid" : NumberInt(1),
"content" : "医学伦理学的公正原则",
"reorderFlag" : NumberInt(0)
},
{
"qid" : NumberInt(2),
"content" : "制定有关人体实验的基本原则",
"reorderFlag" : NumberInt(1)
}
]
}

可以看到, 其执行结果是, 对数组内的每一个子元素, 执行 $elemMatch 匹配, 可以进行多个条件的匹配。

找出数组中, qid=1 或者 reorderFlag=0的记录

数组整体能满足以下2个条件:

{ "qList.qid": 1, "qList.reorderFlag": 0}

执行的主体是 qList, 要求: 有某些子元素满足 qid=1, 也要有某些子元素满足 reorderFlag=0`。

查询结果是:

{
"_id" : "123",
"name" : "人文医学",
"qList" : [
{
"qid" : NumberInt(1),
"content" : "医学伦理学的公正原则",
"reorderFlag" : NumberInt(1)
},
{
"qid" : NumberInt(2),
"content" : "制定有关人体实验的基本原则",
"reorderFlag" : NumberInt(0)
}
]
}
{
"_id" : "124",
"name" : "人文医学2",
"qList" : [
{
"qid" : NumberInt(1),
"content" : "医学伦理学的公正原则",
"reorderFlag" : NumberInt(0)
},
{
"qid" : NumberInt(2),
"content" : "制定有关人体实验的基本原则",
"reorderFlag" : NumberInt(1)
}
]
}

可以看到, 其执行结果是, 对数组进行匹配, 其中需要有子元素 满足 "qList.qid": 1, 还需要有子元素 满足 "qList.qid": 1, , 适合进行单个条件的匹配。

如果是单个条件匹配, 则以下方式结果是一样的。

{ "qList.qid": 1}

  或者

{ "qList": { $elemMatch: { "qid": 1} } }

  查询的结果都是2条记录

mongodb对数组中的元素进行查询详解的更多相关文章

  1. MongoDB 更新数组中的元素

    本文记录如何更新MongoDB Collection 中的Array 中的元素.假设Collection中一条记录格式如下: 现要删除scores 数组中,"type" 为 &qu ...

  2. PHP实现查询两个数组中不同元素的方法

    以下实例讲述了PHP实现查询两个数组中不同元素的方法.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  3. MongoDB 学习笔记之 从数组中删除元素和指定数组位置

    从数组中删除元素: 从数组中删除单个元素: db.ArrayTest.updateOne({ "name" : "Bill"},{$pop: {"ad ...

  4. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  5. php 去除数组中重复元素

    去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...

  6. 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件

      将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...

  7. 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。

    功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...

  8. 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  9. 数组中的元素 增加push用法 unshift() 方法 和减少pop() 方法 shift() 和其他位置增删 splice() 方法 join() 方法 reverse() 方法 sort() 方法

    push用法 push 英 [pʊʃ] 美 [pʊʃ] vt. 推,推动; vt. 按; 推动,增加; 对…施加压力,逼迫; 说服; n. 推,决心; 大规模攻势; 矢志的追求 定义和用法 push( ...

随机推荐

  1. Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)

    本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...

  2. BZOJ3262:陌上花开

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  3. Proxmox qm命令应用实例

          1) 把一个物理硬盘增加到VM上qm set 105 --virtio1 /dev/sdb     <-- 表示把/dev/sdb物理硬盘以 virtio 接口类型增加至 105 的 ...

  4. WPF中DataGrid控件的过滤(Filter)性能分析及优化

    DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程:      用户输入过滤条件       ...

  5. ubuntu lts install licode tag pre-v5.4

    1. Requirements Ubuntu 14.04 LTS 2. Clone Licode codeYou first need to clone our code from github.Yo ...

  6. Pager分页

    分页组件: /// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary&g ...

  7. springboot实现定时任务的一种方式

    参考:https://www.yoodb.com/news/detail/1205 目前觉得这种还是很好用,所以就记录. 最好新建一个项目测试: 1. pom中添加 <dependency> ...

  8. 此上下文中不允许异步操作。启动异步操作的页必须将 Async 特性设置为 true,并且异步操作只能在 PreRenderComplete 事件之前的页上启动。

    <%@ Page Language="C#" AutoEventWireup="true" ...... Async="true" % ...

  9. 2016.2.13 (年初六) oracle两张表update方法

    A表customers和B表tmp_cust_city有3个相同字段, customer_id,city_name,customer_type 现要根据b表更新a表 更新一个字段情况: update ...

  10. python_class21

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...