在数组上使用Mongoose Populate Virtuals

时间:2017-10-25 22:51:37

标签: mongodb mongoose mongoose-populate

这是我的Follow型号

let mongoose = require('mongoose');
let Schema = mongoose.Schema;

let FollowSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  followers: [{
    type: Schema.Types.ObjectId,
    ref: 'Card'
  }],
  following: [{
    type: Schema.Types.ObjectId,
    ref: 'Card'
  }]
});

module.exports = mongoose.model('Follow', FollowSchema);

这是我的Card型号

let mongoose = require('mongoose');
let Schema = mongoose.Schema;

let CardSchema = new Schema({
  title: String,
  content: String,
  likes: Number,
  createdById: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  }
});

module.exports = mongoose.model('Card', CardSchema);

DB中的典型Follow模型将具有以下内容:

{
    "_id" : ObjectId("59f0eef155ee5a5897e1a66d"),
    "user" : ObjectId("59e3e617149f0a3f1281e849"),
    "following" : [
        ObjectId("59e21942ca5652efc4ca30ab"),
        ObjectId("59e13b2dca5652efc4ca2cf5")
    ]
}

这是我正在尝试做的事情:

  • 要获取您关注的所有帖子,请在user模型中找到Follow,并在ObjectId数组中找到following,从{{1模型,匹配Card字段。

使用Mongoose Populate Virtuals,如何进行架构关系并最终填充查询?

或者,我如何创建和查询有或没有虚拟的关系?

0 个答案:

没有答案