Join two collections in MongoDB

前端 未结 5 709
旧时难觅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:19

    The mongodb is not relational database -- so it is not possible to any kind of joins here. Joins is hard to scale.

    The common way in mongodb to achieve join is data denormalization. In your case you could denormalize author name into book table. Then your query will not require join. This is schema example:

    book
    {
      _id,
      name,
      editions,
      characters,
      author_name
    } 
    

    Keep in the mind that you will need to update author_name in books collection each time when you update author collection.

    Another solution -- additional request for name of author for each book, but it will work much slower.

提交回复
热议问题