关于refs的MongoDB(mongoose)

时间:2018-01-14 13:30:50

标签: mongodb refs

谁可以通过示例解释如何从另一个模式获取用户数据(例如useravatar)  虽然我读到了关于refs而且我无法理解。

这是我的代码,但我不仅要发回文章,还要发送有关作者的个人资料数据。我怎样才能做到这一点 ?我已经有了authorID。

router.post('/get-article', (req, res) => {
    const { id, authorID } = req.body.data;
    Article.findByIdAndUpdate({ _id: id }, { $inc: { "pageview": 1 } }, (err, article) => {
            if (err) return res.status(400).json({ NotFound: "Article Not Found" })
            res.json({ article })
        })
})

文章架构

const schema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: { type: String, required: true },
    image: { type: String, required: true },
    content: { type: String, required: true },
    email: { type: String, required: true },
    author: { type: String, required: true, index: true },
    added: { type: Date, default: Date.now },
    pageview: { type: Number, default: 0 }
});

用户架构

const schema = new mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  username: { type: String, required: true },
  email: { type: String, required: true },
  facebookId: { type: String },
  githubId: { type: String },
  googleId: { type: String },
  useravatar: { type: String, required: true },
  userip: { type: String, required: true },
  accessToken: { type: String },
  date: { type: Date, default: Date.now }
});

0 个答案:

没有答案