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( ...
随机推荐
- Python函数-map()
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回.如下: def ...
- 蓝桥杯 算法训练 ALGO-142 P1103
算法训练 P1103 时间限制:1.0s 内存限制:256.0MB 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间 ...
- codeforce -39E-What Has Dirichlet Got to Do with That?(博弈+dfs)
You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1 ...
- L2-014. 列车调度(set的使用,最长递增子序列)
L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...
- rem怎么计算
px:相对长度单位.像素px是相对于显示器屏幕分辨率而言的 em:相对单位,继承父节点(层层继承,传递)基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px ...
- django的settings.py设置session
############ # SESSIONS # ############ SESSION_CACHE_ALIAS = 'default' # Cache to store session data ...
- qt安装必要的库 qt开源安装包下载
yum install mesa-libGL-devel mesa-libGLU-devel #yum install freeglut-devel http://www.qt.io/download ...
- 如何设置linux在出现kernel panic后自动重启 (ZT)
Automatic reboot after Linux kernel panic http://www.syn-ack.org/centos-linux/automatic-reboot-after ...
- 部署和调优 2.8 mysql主从配置-2
配置主从准备工作 在主上创建一个测试的数据库 首先登录主的mysql,或者用绝对路径 /usr/local/mysql/bin/mysql mysql > create database db1 ...
- GridControl 隐藏Drag a column header here to group by that column
解决方案: 打开设计器,找到OptionsView,往下拉设置showGroupPanel为false