Join two collections in MongoDB

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

    I need how to do it using mapreduce functions.

    The db object has been deprecated for a long time in MRs as such it is impossible to source two tables at once within an MR.

    There is another solution though: two MRs. You run a MR on the first collection first outputting to the needed collection and then you use a second MR to output to that very same collection using an out option like reduce or merge to "join" to two collections together.

    Of course this is slow so the better way is to not do it. As for:

    select book.name,book.editions,book.characters,author.name 
    from dbo.book book 
    inner join dbo.author author on book.name=author.works_written
    

    This query can be with streaming a cursor from the book collection and then pining the DB very quickly each book you iterate through (it's ok to do this in MongoDB) grabbing the authors details.

    You can also get a set of author ids from the books and then query the authors collection all at once and sort the two out on client side.

提交回复
热议问题