0

I want to join more than two collections in MongoDB . Is it possible to join?

I have 2 collections and I want to join them based by their ids news collection

{
    "lang": "ru",
    "news": [{
        "title": "title",
        "content": "desc",
        "image": "path",
        "categoryId": {
            "$oid": "6171499e0ba3d75082823c9a"
        },
        "createdAt": {
            "$date": "2021-10-21T12:30:33.215Z"
        },
        "_id": {
            "$oid": "61715d7990905adefcf314d1"
        }
    }]
}

and I have got newscategory collection like that


[
{
    "lang": "ru",
    "categories": [{
        "name": "sdfsdsdcsdf",
        "_id": {
            "$oid": "6171499e0ba3d75082823c9a"
        }
    }]
},
{
    "lang": "en",
    "categories": [{
        "name": "ooo",
        "_id": {
            "$oid": "61712980f8ee795c6a569dcb"
        }
    }, {
        "name": "yuy",
        "_id": {
            "$oid": "61712980f8ee795c6a569dcc"
        }
    }, {
        "name": "rtyrty",
        "_id": {
            "$oid": "61712980f8ee795c6a569dcd"
        }
    }]
}
]

Expected Output:

[
  {
    "_id": "61715d7990905adefcf314d0",
    "lang": "ru",
    "news": [
      {
        "title": "sdf",
        "content": "sdf",
        "image": "asdas",
        "categoryId": "6171499e0ba3d75082823c9a",
        "caregory": [
          {
            "name": "sdfsdsdcsdf",
            "_id": "6171499e0ba3d75082823c9a"
          },
         ]

        "createdAt": "2021-10-21T12:30:33.215Z",
        "_id": "61715d7990905adefcf314d1"
      }
    ],

Match is based on both "category_id" and "categories._id".

How can I achieve result like that ?

  • Does this answer your question? [How do I perform the SQL Join equivalent in MongoDB?](https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb) – Dylan Lee Oct 21 '21 at 14:13
  • I tried that but didn't got expected result as it joined other categories as well – Azad Samedov Oct 21 '21 at 14:14
  • Does this answer your question? [MongoDB: Combine data from multiple collections into one..how?](https://stackoverflow.com/questions/5681851/mongodb-combine-data-from-multiple-collections-into-one-how) – BroscR Oct 21 '21 at 14:16

0 Answers0