【问题标题】:Mongoose populate not returning results猫鼬填充不返回结果
【发布时间】:2015-08-26 00:24:30
【问题描述】:

我正在尝试使用填充来返回引用到邮票模型的结果,在邮票的用户数组下,但由于某种原因,当我在数据库中看到邮票中的邮票 ID 列表时,它不会返回任何结果数组...

这是我的代码:

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) {

这是用户架构

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

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    populate 的“查询”形式不将数组作为参数,而是对象:

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

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2016-04-28
      • 2023-03-21
      相关资源
      最近更新 更多