'How can join dynamic collection in mongodb
I need to join with a collection that is selected from the field
collection: master
| _id | collection | refId |
|---|---|---|
| M101 | related_a | A101 |
| M102 | related_b | B101 |
collection: related_a
| _id | Name |
|---|---|
| A101 | A-111 |
| A102 | A-222 |
collection: related_b
| _id | Name |
|---|---|
| B101 | B-111 |
| B102 | B-222 |
Expected Output
| _id | collection | relatedItem |
|---|---|---|
| M101 | related_a | {refId:A101, name:A-111} |
| M102 | related_b | {refId:B101, name:B-111} |
db.master.aggregate([
{
$lookup: {
from: ,// Base on collection field value
localField: 'refId',
foreignField: '_id',
as: 'relatedItem'
}
}
]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
