mongodb对数组中的元素进行查询详解
原文链接: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对数组中的元素进行查询详解的更多相关文章
- MongoDB 更新数组中的元素
本文记录如何更新MongoDB Collection 中的Array 中的元素.假设Collection中一条记录格式如下: 现要删除scores 数组中,"type" 为 &qu ...
- PHP实现查询两个数组中不同元素的方法
以下实例讲述了PHP实现查询两个数组中不同元素的方法.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- MongoDB 学习笔记之 从数组中删除元素和指定数组位置
从数组中删除元素: 从数组中删除单个元素: db.ArrayTest.updateOne({ "name" : "Bill"},{$pop: {"ad ...
- 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...
- php 去除数组中重复元素
去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...
- 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件
将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...
- 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。
功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...
- 【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 ...
- 数组中的元素 增加push用法 unshift() 方法 和减少pop() 方法 shift() 和其他位置增删 splice() 方法 join() 方法 reverse() 方法 sort() 方法
push用法 push 英 [pʊʃ] 美 [pʊʃ] vt. 推,推动; vt. 按; 推动,增加; 对…施加压力,逼迫; 说服; n. 推,决心; 大规模攻势; 矢志的追求 定义和用法 push( ...
随机推荐
- bzoj 4300 绝世好题——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4300 考虑 dp[ i ] 能从哪些 j 转移过来,就是那些 a[ j ] & a[ ...
- HDU4006(小根堆)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- 2015.4.21 SetWindowPos函数用法
定义:[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, int hWndl ...
- 清除SUN服务器部件的suspect状态
对于suspect状态的部件,可以用setchs命令清除其状态.如果ScApp的版本在5.20.15之前,需要进入service模式后才能使用setchs命令.如果ScApp版本 升级到5.20.15 ...
- When install ”matplotlib” with ”pip”, if you get the following error, it means the “freetype” and “png” libraries needed by matplotlib are not installed:
============================================================================ * The following require ...
- 导入android studio项目,编译失败
使用android studio 打开studio 工程,编译的时候报错: “ INFO - .project.GradleProjectResolver - Gradle project resol ...
- Linux-CentOS 学习的坎坷路 (一) 网络配置篇
自己学习的地址:http://www.imooc.com/view/175 学到2.8章节,配置IP这一块,妈蛋,他直接跳过了,都不知道怎么配置,无奈,只能Search 先是找到配置IP的方法: ht ...
- Android Notification通知
/** * 在状态栏显示通知 */ private void showNotification(){ // 创建一个NotificationManager的引用 NotificationManager ...
- __tostring和__invoke 方法
首先放上代码: <?php class MagicTest{ //__tostring会在把对象转换为string的时候自动调用 public function __tostring() { r ...
- HBase 协处理器统计行数
环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...