开发者

selecting all the fields in a row using mapReduce

I am using mongoose with nodejs. I am using mapReduce to fetch data grouped by a field.So all it gives me as a collection is the key with the grouping field only from every row of database.

I need to fetch all the fields from the database grouped by a field and sorted on the basis of another field.e.g.: i have a database having details of places and fare for travelling to those places and a few other fields also.Now i need to fetch the data in such a way that 开发者_如何学Pythoni get the data grouped on the basis of places sorted by the fare for them. MapReduce helps me to get that, but i cannot get the other fields.

Is there a way to get all the fields using map reduce, rather than just getting the two fields as mentioned in the above example??


I must admit I'm not sure I understand completely what you're asking. But maybe one of the following thoughts helps you:

either) when you iterate over your mapReduce results, you could fetch complete documents from mongodb for each result. That would give you access to all fields in each document for the cost of some network traffic.

or) The value that you send into emit(key, value) can be an object. So you could construct a value object that contains all your desired fields. Just be sure to use the exactly same object structure for your reduce method's return value.

I try to illustrate with an (untested) example.

map = function() {
   emit(this.place, 
        {
           'field1': this.field1, 
           'field2': this.field2, 
           'count' : 1 
        });
}

reduce = function(key, values) {
   var result = { 
       'field1': values[0].field1, 
       'field2': values[0].field2, 
       'count' : 0 };

   for (v in values) {
       result.count += values[v].count;
   }

   return obj;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜