我有一个带有过滤器的列表对象。我需要创建一个聚合以基于过滤器过滤列表。 这是一个列表对象示例
[
{
"_id": "5c484ec4cb1e150b1efce101",
"details": {
"title": "Villa 1",
"description": "Description about Villa 1"
},
"options": {
"features": [
"5c482b1ff9f18807694040c6",
"5c482b2ef9f18807694040c7",
"5c482b3af9f18807694040c8",
"5c482b49f9f18807694040c9"
],
"filters": [
{
"filter": "5c482c2c2abe2f07ccd5d428",
"value": 7
},
{
"filter": "5c482c392abe2f07ccd5d429",
"value": 3
},
{
"filter": "5c482c462abe2f07ccd5d42a",
"value": 5
},
{
"filter": "5c482c4d2abe2f07ccd5d42b",
"value": 2
},
{
"filter": "5c482c562abe2f07ccd5d42c",
"value": true
},
{
"filter": "5c482c612abe2f07ccd5d42d",
"value": true
}
]
},
"memberOnly": false,
"feature": false,
"active": true,
"Category": "5c45af3b5ccf2c20833a547a"
},
{
"_id": "5c484ec4cb1e150b1efce101",
"details": {
"title": "Villa 2",
"description": "Description about Villa 2"
},
"options": {
"features": [
"5c482b1ff9f18807694040c6",
"5c482b2ef9f18807694040c7",
"5c482b3af9f18807694040c8",
"5c482b49f9f18807694040c9"
],
"filters": [
{
"filter": "5c482c2c2abe2f07ccd5d428",
"value": 5
},
{
"filter": "5c482c392abe2f07ccd5d429",
"value": 7
},
{
"filter": "5c482c462abe2f07ccd5d42a",
"value": 2
},
{
"filter": "5c482c4d2abe2f07ccd5d42b",
"value": 6
},
{
"filter": "5c482c562abe2f07ccd5d42c",
"value": true
},
{
"filter": "5c482c612abe2f07ccd5d42d",
"value": true
}
]
},
"memberOnly": false,
"feature": false,
"active": true,
"Category": "5c45af3b5ccf2c20833a547a"
}
]
我需要猫鼬聚合才能基于过滤器及其值在对象之间进行过滤
示例:
我需要过滤具有
的列表
值为$ gte:4
的过滤器“ 5c482c2c2abe2f07ccd5d428”
值为$ gte:3
的过滤器“ 5c482c4d2abe2f07ccd5d42b”
具有真实值的过滤器“ 5c482c612abe2f07ccd5d42d”
我过滤预期的结果应该是
[
{
"_id": "5c484ec4cb1e150b1efce101",
"details": {
"title": "Villa 1",
"description": "Description about Villa 1"
},
"options": {
"features": [
"5c482b1ff9f18807694040c6",
"5c482b2ef9f18807694040c7",
"5c482b3af9f18807694040c8",
"5c482b49f9f18807694040c9"
],
"filters": [
{
"filter": "5c482c2c2abe2f07ccd5d428",
"value": 7
},
{
"filter": "5c482c392abe2f07ccd5d429",
"value": 3
},
{
"filter": "5c482c462abe2f07ccd5d42a",
"value": 5
},
{
"filter": "5c482c4d2abe2f07ccd5d42b",
"value": 2
},
{
"filter": "5c482c562abe2f07ccd5d42c",
"value": true
},
{
"filter": "5c482c612abe2f07ccd5d42d",
"value": true
}
]
},
"memberOnly": false,
"feature": false,
"active": true,
"Category": "5c45af3b5ccf2c20833a547a"
}
]
答案 0 :(得分:0)
解决方案是
Listing.find({
$and: [{
'options.filters': {
$elemMatch: {
filter: this.mongoose.Types.ObjectId('5c482c2c2abe2f07ccd5d428'),
value: {$gte: 4}
}
}
}, {
'options.filters': {
$elemMatch: {
filter: this.mongoose.Types.ObjectId('5c482c4d2abe2f07ccd5d42b'),
value: {$gte: 3}
}
}
}, {
'options.filters': {
$elemMatch: {
filter: this.mongoose.Types.ObjectId('5c482c612abe2f07ccd5d42d'),
value: true
}
}
});