// ----------------------------------------------
"_id" : ObjectId("5eb6bbdbc95fdd10d0f6d220"),
"name" : "小红",
"education" : "本科",
"experiences" : [
"profession" : "销售",
"ability" : [
"沟通",
"财务计算"
"profession" : "采购员",
"ability" : [
"英语",
// ----------------------------------------------
"_id" : ObjectId("5eb6bdbbc95fdd10d0f6d23f"),
"name" : "小三",
"education" : "大专",
"experiences" : [
"profession" : "行政助理",
"ability" : [
接下来我想查询出所有学历为“本科”的阅历(experiences)列表,并把它们合并成一个数组返回,如:
"_id" : "1",
"experiences" : [
{ "profession" : "销售", "ability" : ["沟通", "财务计算"] },
{ "profession" : "采购员", "ability" : ["英语", "统计"]},
{ "profession" : "程序员", "ability" : ["java", "nodejs", "golang"]}
使用Aggregate来实现。
db.getCollection("user").aggregate(
"$match" : { "education" : "本科" }
"$group" : {
"_id" : "1",
"experiences" : {
"$addToSet" : "$experiences"
"$project" : {
"experiences" : {
"$reduce" : {
"input" : "$experiences",
"initialValue" : [],
"in" : { "$concatArrays" : ["$$value", "$$this"] }
$match: 选择器,筛选的条件,这里查询出学历(education)为本科的数据。
$group: 相当于sql中的group by, 例子中的"_id"只是用于分组的标识,“experiences“设置分组返回到哪个字段上,相当于as。
$addToSet: 不重复添加到数组。
到这里,会把学历为“本科”的阅历全部查出来,数据是这样子:
"_id" : "1",
"experiences" : [
{"profession" : "销售", "ability" : [ "沟通", "财务计算" ]},
{"profession" : "采购员", "ability" : [ "英语", "统计" ]}
{"profession" : "程序员", "ability" : ["java", "nodejs", "golang"]}
但这还不是我们要的结果,使用$project再次处理,所有experiences追加
。
"$project" : {
"experiences" : {
"$reduce" : {
"input" : "$experiences",
"initialValue" : [],
"in" : { "$concatArrays" : ["$$value", "$$this"] }
$project: 接受一个文档,该文档可以指定包含字段、禁止显示id字段、添加新字段以及重置现有字段的值。或者,可以指定排除字段。
$reduce:将"in"里的表达式应用到"input"数组里,并将它们合并成一个数组。
"in"里面的表达式可以很灵活的对数组操作,这里用的$concatArrays将experiences[]字段里的元素连接起来。
最后结果:
"_id" : "1",
"experiences" : [
{ "profession" : "销售", "ability" : ["沟通", "财务计算"] },
{ "profession" : "采购员", "ability" : ["英语", "统计"]},
{ "profession" : "程序员", "ability" : ["java", "nodejs", "golang"]}
https://docs.mongodb.com/manual/reference/operator/aggregation/reduce/