在不同位置填充mongoose subdoc

时间:2015-11-30 21:02:01

标签: mongodb mongoose mongoose-populate

我使用我的用户模型填充子文档。现在,填充的所有信息都放在user_list[x]._id下,例如user_list[0]._id.displayName,但我希望这样的数据可用:user_list[0].displayName

是否可以告诉mongoose使用user_list[x]数据的填充数据扩展user_list[x]._id而不是将其放在_id属性本身?

/// model

'use strict';

var mongoose = require('mongoose'),
  Schema = mongoose.Schema,
  deepPopulate = require('mongoose-deep-populate')(mongoose);


var user_list_schema = new Schema({
   _id:{
    type: Schema.ObjectId,
    ref: 'User'
  },
  soft_delete:{
    type:Boolean
  }

 });

var BusinessSchema = new Schema({

  title: {
    type: String,
    default: '',
    trim: true,
    required: 'Title cannot be blank'
  },

  user_list:[user_list_schema],
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  }
});

mongoose.model('Business', BusinessSchema);

///controller

Business.findById(id).populate('user', 'displayName').populate('user_list._id').exec(function (err, business) {
if (err) {
  return next(err);
} else if (!business) {
  return res.status(404).send({
    message: 'No business with that identifier has been found'
  });
}
req.business = business;
next();
});

0 个答案:

没有答案