Mongodb查询投射中的$elemMatch

本文研究查询过程中,定义数组返回结果的另外一个操作符$elemMatch。

定义

投射中elemMatch与操作符号一样,都是返回数组中第一个符合查询条件的数据。

但两个操作命令也有差异。使用符号时,用户只能通过查询条件来过滤数组元素。而使用elemMatch时,用户可以显示的定义更多的数组过滤条件。同时$elemMatch也支持文档数组中多个字段的查询条件定义。

字段顺序

在投射中,无论用户定义返回多少个字段,使用elemMatch和操作符号时, 4.4以后版本的mongodb都会将符合查询条件的数组结果,置于所有字段的后面。

向player集合插入一条数据

复制代码
db.players.insertOne({
    name: "player1",
    games: [
        { game: "abc", score: 8},
        { game: "xyz", score: 5}
        ],
    joined: new Date("2020-01-01"),
    lastLogin: new Date("2020-05-01")
})

使用$elemMatch构建查询语句,查找出score大于5的数据。其中指定返回字段games, joined, lastLogin

复制代码
db.players.find(
    {},
    {
        games: {$elemMatch: {"score": {$gt: 5}}},
        joined: 1, 
        lastLogin: 1
    })

查询结果中, games字段排列到了joined和lastLogin后。而在4.2及以前的mongodb版本中,games字段会按照用户查询投射中定义的字段顺序返回。

复制代码
{
	"_id" : ObjectId("65b594f1374038b32213a3e5"),
	"joined" : ISODate("2020-01-01T08:00:00.000+08:00"),
	"lastLogin" : ISODate("2020-05-01T08:00:00.000+08:00"),
	"games" : [
		{
			"game" : "abc",
			"score" : 8
		}
	]
}

使用限制

  • mongodb 视图中,不支持使用$elemMatch查询。
  • elemMatch中不能使用text查询表达式。

应用举例

构建测试集合

向schools集合中,插入带有数组students的4条数据

复制代码
db.schools.insertMany([
    {
     _id: 1,
     zipcode: "63109",
     students: [
                  { name: "john", school: 102, age: 10 },
                  { name: "jess", school: 102, age: 11 },
                  { name: "jeff", school: 108, age: 15 }
               ]
    },
    {
     _id: 2,
     zipcode: "63110",
     students: [
                  { name: "ajax", school: 100, age: 7 },
                  { name: "achilles", school: 100, age: 8 },
               ]
    },
    {
     _id: 3,
     zipcode: "63109",
     students: [
                  { name: "ajax", school: 100, age: 7 },
                  { name: "achilles", school: 100, age: 8 },
               ]
    },
    {
     _id: 4,
     zipcode: "63109",
     students: [
                  { name: "barney", school: 102, age: 7 },
                  { name: "ruth", school: 102, age: 16 },
               ]
    }
])

单字段查询

查询出zipcode是63109的数据,在返回结果中,显示第一个school是102的学生数据。

复制代码
db.schools.find(
    {zipcode: "63109"},
    {students: {$elemMatch: { school: 102}}}
)

返回结果

复制代码
/* 1 */
{
	"_id" : 1,
	"students" : [
		{
			"name" : "john",
			"school" : 102,
			"age" : 10
		}
	]
},

/* 2 */
{
	"_id" : 3
},

/* 3 */
{
	"_id" : 4,
	"students" : [
		{
			"name" : "barney",
			"school" : 102,
			"age" : 7
		}
	]
}

多字段查询

构建查询语句,返回zipcode是63109的数据。要求结果中返回第一个字段school是102,并且age大于10的数据。

复制代码
db.schools.find(
    {zipcode: "63109"},
    {students: {$elemMatch: { school: 102, age: {$gt: 10}}}}
)

返回结果

复制代码
/* 1 */
{
	"_id" : 1,
	"students" : [
		{
			"name" : "jess",
			"school" : 102,
			"age" : 11
		}
	]
},

/* 2 */
{
	"_id" : 3
},

/* 3 */
{
	"_id" : 4,
	"students" : [
		{
			"name" : "ruth",
			"school" : 102,
			"age" : 16
		}
	]
}
相关推荐
TDengine (老段)44 分钟前
TDengine 时间函数 WEEKDAY() 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
TDengine (老段)1 小时前
从 ETL 到 Agentic AI:工业数据管理变革与 TDengine IDMP 的治理之道
数据库·数据仓库·人工智能·物联网·时序数据库·etl·tdengine
LQ深蹲不写BUG3 小时前
MySql的事务机制
数据库·mysql
逼子格4 小时前
【Proteus仿真】定时器控制系列仿真——秒表计数/数码管显示时间
数据库·单片机·嵌入式硬件·51单片机·proteus·定时器·硬件工程师
stein_java5 小时前
Mybatis-7 XML映射器
数据库·sql·mybatis
xhbh6665 小时前
开发效率翻倍:资深DBA都在用的MySQL客户端利器
数据库·mysql·数据库连接工具·mysql 连接工具
LJC_Superman5 小时前
Web与Nginx网站服务
运维·服务器·前端·网络·数据库·nginx·vim
java水泥工5 小时前
校园管理系统|基于SpringBoot和Vue的校园管理系统(源码+数据库+文档)
数据库·vue.js·spring boot
高山有多高6 小时前
详解文件操作
c语言·开发语言·数据库·c++·算法