Mongoose populate not returning results

660 Views Asked by At

I am trying to use populate to return results that are ref to the Stamp model, under the users array of stamps but for some reason it does not return any results when I see in the database a list of stamp ids in the stamps array...

Here is my code:

var selectQuery = "_id name";
    var populateQuery = [{path:'stamps', select: selectQuery, model: 'Stamp', }];
    User.findOne({_id: userId}).populate(populateQuery).sort({date: -1}).skip(count).limit(100).exec(function(err, results) {
        if(err) {

Here is the User Schema

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.Schema.Types.ObjectId,
var Stamp = require('../models/stamp.js');

var User = new Schema({ 
    name: { type: String},

    stamps: [{ type: ObjectId, ref: 'Stamp' }],
1

There are 1 best solutions below

0
On BEST ANSWER

The "query" form of populate doesn't take an array as argument, but an object:

// `model` can be left out as Mongoose will look that up in the schema
var populateQuery = { path : 'stamps', select : selectQuery };