Been reading about map/reduce here and on other sites and I’m still not sure if it’s the right or wrong way to solve this problem.
I have three models:
User -- embeds many SearchRequests
SearchRequest -- references 1 Item
Item
What I need to do is get a bit of information from each model and then eliminate any results that don’t meet criteria. Is this a map/reduce?
The map I want:
user.email, user.name, item.name, item.amount,
item.url, searchRequest.amountCutoff
Then I think I’d reduce and elminate any records where:
item.amount > searchRequest.amountCutoff
The thing I’m having trouble with is how to get the item.name, item.amount in the map. This is what I have so far (map would be called on the Users collection):
map = "function() { if (this.search_requests)
for (var ar in this.search_requests) {
// this part needs help ---vvvv
var theItem= items.where(_id: this.search_requests[ar].item_id)
emit ( { },
{ this.email,
this.name,
this.search_requests[ar].item_id,
this.search_requests[ar].amountCutoff,
theItem.name,
theItem.amount
}
}
} "
^^ The syntax on that may be incorrect. I’m still learning. It almost looks like I need the mongo equivalent of LINQs range variable in there… not sure if that’s possible.
So my question: is it possible (or smart) to access another collection that isn’t the “mapping” collection in a map function?
AFAIK, unfortunately, map/reduce doesn’t support for more than one collection for the moment.
Since you know the criteria item.count > n, create a index on it and query out the documents. which should contains user’s reference, say userid.
which could be done by running a javascript in mongoshell or you can write code in your environment.