Skip to content

Instantly share code, notes, and snippets.

@james4388
Created June 15, 2019 01:17
Show Gist options
  • Save james4388/9d453b62b67780001db528dce8d16b30 to your computer and use it in GitHub Desktop.
Save james4388/9d453b62b67780001db528dce8d16b30 to your computer and use it in GitHub Desktop.
Aggregate deep nested mongo
db.getCollection('article').aggregate([
{$match: {$or: [
{"view.sections.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}},
{"view.sections.steps.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}}
]}},
{$project: {_id: 1}
])
db.getCollection('article').aggregate([
{$match: {$or: [
{"view.sections.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}},
{"view.sections.steps.parts.embed.provider_name": {$in: ['Commerce Product', 'Image With Hotspots', 'Commerce Product Group']}}
]}},
{$project: {
"provider_names": {
$reduce: {
input: {$concatArrays: [
{$ifNull: ["$view.sections.parts.embed.provider_name", []]},
{$ifNull: ["$view.sections.steps.parts.embed.provider_name", []]}
]},
initialValue: [],
in: { $concatArrays : ["$$value", "$$this"] }
}
}
}}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment