在猫鼬聚合期间保持原始字段完整或通过管道传递
Keeping original fields intact or piping them through during mongoose aggregation
db.testA.aggregate([
{
"$lookup": {
"from": "testB",
"localField": "_id",
"foreignField": "aId",
"as": "join"
}
},
{
$project: {
count: {
$size: "$join"
}
}
},
{
$set: {
favorite: {
$switch: {
branches: [
{
case: {
$gte: [
"$count",
1
]
},
then: true
},
{
case: {
$gte: [
"$count",
0
]
},
then: false
},
],
default: false
}
}
}
}
])
我有这个聚合函数。我想保留这些字段,但我不确定它们为什么消失了。
db={
"testA": [
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
}
],
"testB": [
{
"_id": ObjectId("60508f4152a2ab598a013bbb"),
"aId": ObjectId("60508f2852a2ab598a013bba")
},
{
"_id": ObjectId("60508f4552a2ab598a013bbc"),
"aId": ObjectId("60508f2852a2ab598a013bba")
},
{
"_id": ObjectId("605090a452a2ab598a013bbd"),
"aId": ObjectId("60508eeb52a2ab598a013bb9")
}
]
}
这是我的数据库。本质上,我希望通过管道传递消息、field1、field2、field3,以便最后得到 6 个字段而不是 2 个字段。
这是我得到的结果:
[
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"count": 1,
"favorite": true
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"count": 2,
"favorite": true
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"count": 0,
"favorite": false
}
]
https://mongoplayground.net/p/KjquoKCUw4P
如果不清楚,这是我想要的:
[
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"count": 1,
"favorite": true,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"count": 2,
"favorite": true,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"count": 0,
"favorite": false,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
}
]
数据几乎相同,但假设它们都不同。
使用 $addFields 而不是 $project:
{
$addFields: {
count: {
$size: "$join"
}
}
},
db.testA.aggregate([
{
"$lookup": {
"from": "testB",
"localField": "_id",
"foreignField": "aId",
"as": "join"
}
},
{
$project: {
count: {
$size: "$join"
}
}
},
{
$set: {
favorite: {
$switch: {
branches: [
{
case: {
$gte: [
"$count",
1
]
},
then: true
},
{
case: {
$gte: [
"$count",
0
]
},
then: false
},
],
default: false
}
}
}
}
])
我有这个聚合函数。我想保留这些字段,但我不确定它们为什么消失了。
db={
"testA": [
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
}
],
"testB": [
{
"_id": ObjectId("60508f4152a2ab598a013bbb"),
"aId": ObjectId("60508f2852a2ab598a013bba")
},
{
"_id": ObjectId("60508f4552a2ab598a013bbc"),
"aId": ObjectId("60508f2852a2ab598a013bba")
},
{
"_id": ObjectId("605090a452a2ab598a013bbd"),
"aId": ObjectId("60508eeb52a2ab598a013bb9")
}
]
}
这是我的数据库。本质上,我希望通过管道传递消息、field1、field2、field3,以便最后得到 6 个字段而不是 2 个字段。
这是我得到的结果:
[
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"count": 1,
"favorite": true
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"count": 2,
"favorite": true
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"count": 0,
"favorite": false
}
]
https://mongoplayground.net/p/KjquoKCUw4P
如果不清楚,这是我想要的:
[
{
"_id": ObjectId("60508eeb52a2ab598a013bb9"),
"count": 1,
"favorite": true,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab598a013bba"),
"count": 2,
"favorite": true,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
},
{
"_id": ObjectId("60508f2852a2ab665a013bba"),
"count": 0,
"favorite": false,
"message": "hello",
"field1": false,
"field2": 1,
"field3": "something"
}
]
数据几乎相同,但假设它们都不同。
使用 $addFields 而不是 $project:
{
$addFields: {
count: {
$size: "$join"
}
}
},