最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用Mongoose填充嵌套数组

运维笔记admin9浏览0评论

使用Mongoose填充嵌套数组

使用Mongoose填充嵌套数组

我有一个用户架构,如下所示,我正在尝试填充实时项目数组,但我无法弄清楚如何访问它。

const userSchema = new Schema({
  local: {
    email: String,
    username: String,
    password: String,
    liveProjects: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'liveProject'
    }]
  },
  google: {
    googleId: String,
    email: String,
    username: String,
    liveProjects: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'liveProject'
    }]
  }
});

const User = mongoose.model('user', userSchema);
module.exports = User;

如果它没有嵌入我可以使用

User.findById(id).populate('liveProjects').exec((err,projects)=>{});

但是如何才能访问'local.liveProjects''google.liveProjects'以便我可以填充它们?

回答如下:

事实证明它就是这么简单

User.findById(id).populate('local.liveProjects').exec((err,projects)=>{});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论