How to populate after mapReduce?

727 views
Skip to first unread message

Burak Kilic

unread,
Feb 7, 2013, 8:39:42 AM2/7/13
to mongoo...@googlegroups.com
Hi;

I am trying to populate reference fields after mapReduce:

 var o = {};  
  o.query = { 'place' : req.param('place') ,
              '_id' : {'$nin':req.user.ratings}
            }
  o.map = function () { emit(0, {k: this, v: Math.random()})  }
  o.reduce = function(k, v) {
    var a = []
    v.forEach(function(x) {
      a = a.concat(x.a ? x.a : x)
    })
    return {a:a.sort(function(a, b) {
      return a.v - b.v;
    }).slice(0, 10 /*how many records you want*/)}; 
  }
  o.finalize = function(k, v) {
    return v.a.map(function(x) {
      return x.k
    })
  }
  o.out = { replace: 'createdCollectionNameForResults' };


  Venue.mapReduce(o, function (err, results) {
    mongoose.connection.db.collection('createdCollectionNameForResults', function(err, collection) { //query the new map-reduced table
        collection.find({}).populate('place').toArray(function(err, pings) { //only pull in the top 10 results and sort descending by number of pings
               res.send(200, pings)
        });
    });
  })

But I am getting TypeError because collection.find({}) is from MongoDB, not from Mongoose. How can I populate references?

Aaron Heckmann

unread,
Feb 15, 2013, 6:57:40 PM2/15/13
to mongoo...@googlegroups.com
In 3.x you receive a model back from mapReduce you can query the result collection with but it doesn't permit population:

Venue.mapReduce(o, function (err, model) {
  model.find(..).exec(function (err, docs) {
    // perform manual population here
    // docs are plain js objects, not mongoose documents
  })
})

Venue.mapReduce(o, function (err, model) {
  model.find(..).populate({ path: 'some.place', model: 'Place' }).exec(err, pings) {
    console.log(pings)
  })
})

In fact, in 3.6 you'll be able to populate any ad-hoc property you wish as long as you specify the model name.





--
--
http://mongoosejs.com - docs
http://plugins.mongoosejs.com - plugins search
http://github.com/learnboost/mongoose - source code
 
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages