简体   繁体   中英

Mongoose populate not returning results

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' }],

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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM