Created
June 15, 2019 01:17
-
-
Save james4388/9d453b62b67780001db528dce8d16b30 to your computer and use it in GitHub Desktop.
Aggregate deep nested mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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