Still cannot find the correct way to populate fields in the callback mapreduce
See original GitHub issueI 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:
- Created 10 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think I have found the problem. when populating
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
which is exactly what I want. I will make a reformat so that in the end that json looks like this
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.
I use this map for replace values of _id after map/reduce