mongoose得到所有相关的对象

时间:2017-12-21 14:42:06

标签: node.js mongodb mongoose

let Schema = mongoose.Schema

let simpleSchema = new Schema({
    company: { type: Schema.Types.ObjectId, ref: 'Company', require: true }
    ....
})

并且此架构没有anotherSchema引用。但是anotherSchemasimpleSchema引用

let anotherSchema = new Schema({
    simple: { type: Schema.Types.ObjectId, ref: 'Simple', require: true }
    ....
})

所以当我从第一个模式创建模型时

return mongoose.model('Simple', simpleSchema)

我称之为:

simpleSchema.statics.testpopulate = function (id) {
return this.findOne()
  .where('_id').equals(id)
  .populate('company')
  .populate('simple')
  .then((s) => {
    console.log('sc', s.company)
    console.log('sp', s.simple)
    return Promise.resolve(s)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

它只记录公司。我知道这是对的,但我怎么能强迫回归呢?

所以我想要检索所有引用我的简单集合的对象

0 个答案:

没有答案