Join two collections in MongoDB

前端 未结 5 713
旧时难觅i
旧时难觅i 2021-01-23 18:33

Am a beginner in mongoDB. I have two collections Book and author. [name and workswritten] are the common column respectively. Using inner join I have to emit the some columns in

5条回答
  •  星月不相逢
    2021-01-23 19:18

    You can compare below codes for SQL and mongoDB (NoSQL):

    • SQL Code:

       SELECT *, [output array field]
            FROM collection
            WHERE [output array field] IN (SELECT *
                  FROM [collection to join]
                  WHERE [foreignField]= [collection.localField]);
      
    • mongoDB (NoSQL):

       {
          $lookup:
            {
              from: [collection to join],
              localField: [field from the input documents],
              foreignField: [field from the documents of the "from" collection],
              as: [output array field]
            }
       }
      

提交回复
热议问题