Mongo 项目 child object 但道具较少
Mongo project child object but with fewer props
我的 Collection 中有一个类似这样的简单文档,名为 'Partners'
[{
PartnerName: 'Company A',
MarketExpertese: [{Sector: {Code: 1, TotalYears: 1, TotalMonths: 20, TotalClients: 10}},
{Sector: {Code: 2, TotalYears: 2, TotalMonths: 20, TotalClients: 10}},
{Sector: {Code: 3, TotalYears: 3, TotalMonths: 20, TotalClients: 10}}]
}]
所需投影的结果为:
[{
PartnerName: 'Company A',
MarketExpertese: [{SectorCode: 1, TotalYears: 1},
{SectorCode: 2, TotalYears: 2},
{SectorCode: 3, TotalYears: 3}]
}]
我尝试使用地图进行投影,但没有成功。如果我只需要一个 SectorCode 数组(简单数组,例如 {[1, 2, 3]}
)我可以做到,但是因为我需要一个具有 2 个值(sectorCode 和 TotalYears)的道具,所以我的地图失败了...
我能得到的壁橱是这个Mongo Playground
我们需要使用 $unwind
来获取关于数组 MarketExpertese
的文档流,每个文档都将包含该数组的一个元素
然后使用 $project
阶段根据需要格式化输出
然后使用 $group
对我们在第一步展开的文档进行分组
查询可能看起来像这样
db.collection.aggregate([
{
"$unwind": "$MarketExpertese" // First we need to use unwind to get a stream of documents regarding this array, each document will have only one element from that array
},
{
"$project": {
PartnerName: 1,
Taste: {
SectorCode: "$MarketExpertese.Sector.Code",
TotalYears: "$MarketExpertese.Sector.TotalYears"
}
}
},
{
"$group": { // then group the results
"_id": "$_id",
PartenerName: {
"$first": "$PartnerName"
},
Taste: {
"$addToSet": "$Taste"
}
}
}
])
这里是 Mongo Playground
上的一个工作示例
更新
您可以使用 $map 来实现相同的功能
db.collection.aggregate([
{
$project: {
"_id": 0,
"PartnerName": 1,
"Teste": {
"$map": {
"input": "$MarketExpertese",
"as": "elem",
"in": {
SectorCode: "$$elem.Sector.Code",
TotalYears: "$$elem.Sector.TotalYears",
}
}
}
}
}
])
你可以在这里测试Mongo Playground 2
我的 Collection 中有一个类似这样的简单文档,名为 'Partners'
[{
PartnerName: 'Company A',
MarketExpertese: [{Sector: {Code: 1, TotalYears: 1, TotalMonths: 20, TotalClients: 10}},
{Sector: {Code: 2, TotalYears: 2, TotalMonths: 20, TotalClients: 10}},
{Sector: {Code: 3, TotalYears: 3, TotalMonths: 20, TotalClients: 10}}]
}]
所需投影的结果为:
[{
PartnerName: 'Company A',
MarketExpertese: [{SectorCode: 1, TotalYears: 1},
{SectorCode: 2, TotalYears: 2},
{SectorCode: 3, TotalYears: 3}]
}]
我尝试使用地图进行投影,但没有成功。如果我只需要一个 SectorCode 数组(简单数组,例如 {[1, 2, 3]}
)我可以做到,但是因为我需要一个具有 2 个值(sectorCode 和 TotalYears)的道具,所以我的地图失败了...
我能得到的壁橱是这个Mongo Playground
我们需要使用 $unwind
来获取关于数组 MarketExpertese
的文档流,每个文档都将包含该数组的一个元素
然后使用 $project
阶段根据需要格式化输出
然后使用 $group
对我们在第一步展开的文档进行分组
查询可能看起来像这样
db.collection.aggregate([
{
"$unwind": "$MarketExpertese" // First we need to use unwind to get a stream of documents regarding this array, each document will have only one element from that array
},
{
"$project": {
PartnerName: 1,
Taste: {
SectorCode: "$MarketExpertese.Sector.Code",
TotalYears: "$MarketExpertese.Sector.TotalYears"
}
}
},
{
"$group": { // then group the results
"_id": "$_id",
PartenerName: {
"$first": "$PartnerName"
},
Taste: {
"$addToSet": "$Taste"
}
}
}
])
这里是 Mongo Playground
上的一个工作示例更新
您可以使用 $map 来实现相同的功能
db.collection.aggregate([
{
$project: {
"_id": 0,
"PartnerName": 1,
"Teste": {
"$map": {
"input": "$MarketExpertese",
"as": "elem",
"in": {
SectorCode: "$$elem.Sector.Code",
TotalYears: "$$elem.Sector.TotalYears",
}
}
}
}
}
])
你可以在这里测试Mongo Playground 2