question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Still cannot find the correct way to populate fields in the callback mapreduce

See original GitHub issue

I know it’s a new feature, since 3.6. But i have tried everything.

I have a model declared like this

var TagsMapReduce = new Schema({
    _id: {type: Schema.ObjectId, ref: 'Tag'},
    value: {type : Number}
});

mongoose.model('TagsMapReduce', TagsMapReduce);

i have set in my mapreduce object

o.out = { replace: 'TagsMapReduce' }

then I have a mapreduce

PostEntry
  .mapReduce(o, function (err, results) {
    //console.log(results);
    results
    .find()
    //.populate({ path: '_id', model: 'TagsMapReduce', select:'_id name' })
    .exec(function(err, res){
        console.log(res);
//output is [ { _id: 5182be986ae2bec804000006, value: 1 }, { _id: 5182bea86ae2bec804000007, value: 1 } ]

    })
  });

if i uncomment the populate, the result are now showing in place of ids null

[ { _id: null, value: 1 }, { _id: null, value: 1 } ]

and are not populated. I have double checked the name of the model, and the name of the referring schema, everything seems fine.

say tags are

var TagSchema = new Schema({
    name: {type : String, default : '', trim : true, unique: true}
});

mongoose.model('Tag', TagSchema);

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
talamaskacommented, May 7, 2013

I think I have found the problem. when populating

.populate({ path: '_id', model: 'TagsMapReduce', select:'_id name' })

I should define the model to which I want to create the reference, not the model that I want to use as structure for getting the reference.

so if I put

.populate({ path: '_id', model: 'Tag', select:'_id name' })
Mongoose: tags.find({ _id: { '$in': [ ObjectId("517e7149167f889018000005"), ObjectId("517e714f167f889018000006
"), ObjectId("517e7155167f889018000007"), ObjectId("517e7165167f889018000009") ] } }) { fields: { name: 1, _id: 1 }, safe: undefined, lean: true }

//populated tags
[ { _id: { _id: 517e7149167f889018000005, name: 'Buy and Sell' }, value: 2 },
  { _id: { _id: 517e714f167f889018000006, name: 'Fun' }, value: 1 },
  { _id: { _id: 517e7155167f889018000007, name: 'Tutorial' }, value: 1 },
  { _id: { _id: 517e7165167f889018000009, name: 'Important' }, value: 1 } ]

which is exactly what I want. I will make a reformat so that in the end that json looks like this

[ { _id: 517e7149167f889018000005, name: 'Buy and Sell' , count: 2 },
  { _id: 517e714f167f889018000006, name: 'Fun' , count: 1 },
  { _id: 517e7155167f889018000007, name: 'Tutorial' , count: 1 },
  { _id: 517e7165167f889018000009, name: 'Important' , count: 1 } ]

Also I have found that, I have to define a schema for replace with dbref in it, for every mapreduce on every different collection and that i have to define the same model name when populating, it is not working automatically. if i don’t define the dbref in the mapreduce models, and i put the model name just in the populate, nothing happens, I don’t see in the console any request to mongodb showing it is getting the id and the name from another collection. This all seems a non-intentional.
I think if i define a modelname to be used for the population, it should not require a db ref in the model and it should make the ref automatically. that way if you have 10 mapreduces to 10 collection which have same format _id, value, you could define only one model. I guess if you have more than one ObjectID in the mapreduce resulted model, and you want to populate them all , you could use the options passed to populate - path, model, select to create a reference to another collection.

It’s very likely that my thinking is not correct, I’m relatively new in mongodb. So I would appreciate any feedback on the question.

0reactions
markotomcommented, Jul 29, 2013

I use this map for replace values of _id after map/reduce

docs = docs.map(function(doc, key){
  var d   = doc._id;
  d.value = doc.value;
  return d;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose v6.8.2: Query Population
Just call the populate method on the query and an array of documents will be returned in place of the original _id s....
Read more >
Hadoop MapReduce job starts but can not find Map class?
My MapReduce app counts usage of field values in a Hive table. I managed to build and run it from Eclipse after including...
Read more >
JavaScript Map, Reduce, and Filter - JS Array Functions ...
Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation ......
Read more >
How to Use Map, Filter, and Reduce in JavaScript - Code
This article will take a close look at what I like to call the "big three" list operations: map , filter , and...
Read more >
Array.prototype.reduce() - JavaScript - MDN Web Docs
The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found