"populate" 在 mongoose 中是什么意思

what it means by "populate" in mongoose

所以通常什么是“populate”?指的是对数据库的一些操作。

我以前听过,但一直没听懂..

如果您有一个文档指向另一个文档(即包含 ID 引用),populate 将获取引用的文档。

例如,如果您有:

{
  "__id" : "a",
  "className" : "astroPhysics",
  "teacher" : "b"
}

{
  "__id" : "b",
  "teacherName" : "John Smith"
}

获取 a 并填充 teacher 将得到以下结果:

{
  "__id" : "a",
  "className" : "astroPhysics",
  "teacher" : {
      "__id" : "b",
      "teacherName" : "John Smith"
  }
}