Reputation: 2582
I have an existing mongo database where all documents in a collection have a parent pointer (e.g. Parent Links). So walking up the trees are easy and the common case operation.
However, I now want to write a map reduce job that starts at all the tree roots in the collection and produces some data from all the leaf nodes from each tree. So the output collection of the map reduce job should be a series of
{<root_id>, [<information from leafs associated with root_id>, ...]}.
If there are any examples that would be greatly appreciated. My simple attempts to even print the second level of the tree are failing. Can I not run a query inside the map function?
mapf = function() {
db.collection.find({"parent": this._id}).forEach(
// This doesn't seem to work
)
}
Upvotes: 0
Views: 351
Reputation: 45277
Can I not run a query inside the map function?
Kind of, but don't.
One of the key problems is "where does the query go?". That db
reference is to the local database, but that won't work with sharding.
The other problem is that even if it does work, you can get a crazy exponentiation of work which is really dangerous. (how do you handle loops?)
For this type of problem, I would suggest looking at a graph database like Neo4J.
Upvotes: 1