mercredi 17 janvier 2018

MongoDB: can't return random document from collection

I'm working on a word game and am trying to return a random wordpair (my collection) on a page load. I'm using Express and have adapted my code from this tutorial if that's of any use.

A GET request renders my page just fine, and I'm trying to send a random WordPair object alongside the title:

router.get('/', function(req, res, next) {
  res.render('play', { title: 'play', random_wordpair: wordpair_controller.wordpair_random});
});

The wordpair_random function is here inside a controller file I've made (which also successfully manages listing the wordpairs and creating new ones etc).

// Get random WordPair
exports.wordpair_random = function() {
   WordPair.aggregate(
      [{
        $sample: {
          size: 1
        }
      }]
    )
    .exec(function(err, random_wordpair) {
      if (err) {
        return next(err);
      }
      console.log(random_wordpair);
      return random_wordpair;
    });
};

Then inside a play.pug template, I'm simply trying to display this result:

h3 random wordpair selection is: #{random_wordpair}

But all I can see is the function rendered as HTML text. Can anyone tell me what I'm doing wrong?

I also understand looking at the documentation for MongoDB $sample aggregation that I need to be calling my function on the database object, but I've seen various examples and some don't do this. When I try calling db.wordpair.aggregate(...) (or WordPair or wordpairs as it appears in mLab) directly after initializing db in my app.js file, I get undefined errors. My db object doesn't seem to contain the correct data for this request.

Thanks!

enter image description here




Aucun commentaire:

Enregistrer un commentaire