Accessing another collection in MongoDB map decreases

I would like to access another collection from the map function so that I can search by key and do some aggregations. Can I access any collection using db.collection_name

or something similar in BSON code?


source to share

2 answers

If you try this locally, it will work. However, this fails entirely in the case of a folded scenario, because the collection or data in the collection will not be local to the shard.

This is also bad practice because M / R can cause cascading requests that are difficult to track down.

If you encounter this problem, you have several options:

  • Denormalize data . If map

    the collection A

    needs fields x,y,z

    from the collection B

    , copy those fields to A

    . Yes it is not normalized, but MongoDB is not a relational database, it is not meant to be normalized.
  • Multicomponent M / R . In many cases, you can achieve the same result by performing several different operations and writing the results to a single collection. Perhaps you do the M / R on first A

    and then loop through the output and update the data from B

    in a separate script / process.

I've seen both. I've even seen # 2 converted to a simple loop for

that processes both parts at the same time. I have successfully replaced some M / R jobs with simple loops for

and upserts.



This is not possible because it will break in a plastered setting. Map / reduce can only use the collection they are calling on.



All Articles