我被困在MongoDB查询中.我有两个系列.
plans系列:
{
planName: 'TV1',
planFeatures: {
'AA1': true,
'AA2 : false',
'AA3': true,
'AA4': true,
'AA5': false
}
}, {
planName: 'TV2',
planFeatures: {
'AA1': true,
'AA2 : false',
'AA3': false,
'AA4': true,
'AA5': false
}
}, ..........
planFeatures个系列
{
key: 'AA1',
label: 'ALPHA',
}, {
key: 'AA2',
label: 'BETA'
}, ..........
我试图将这两个集合进行聚合,这样planFeatures集合中的key Name字段应该等于planFeatures集合中的关键字段
PaymentPlanFeatures.aggregate([
{
$lookup: {
from: 'plans',
let: { 'kkey': '$key'},
pipeline: [
{ $set: { 'planFeatures5': { '$objectToArray': '$planFeatures' }}},
{
$match: {
$expr: {
$eq: [
'$planFeatures5.k', '$$kkey'
]
}
}
}
],
as: 'paymentplans'
}
}
])
我想我的结果应该是这样的
[
{
"key": "AA1",
"label": "ALPHA",
"paymentplans": [
{
"planName": "TV1",
"planFeatures": {
"AA1": true,
"AA2": true,
"AA3": false
},
"__v": 0,
"planFeatures5": [
{
"k": "AA1",
"v": true
}
]
}
]
}, ..............
]
我在这个问题上纠缠了很长时间.