Accessing another collection in MongoDB map decreases
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 collectionA
needs fieldsx,y,z
from the collectionB
, copy those fields toA
. 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 fromB
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.
+6
source to share