在Virtual Populate中,如何定义foreignField?

In Virtual Populate, how do you define foreignField?

考虑以下代码:

 require("./connection");

// //----------------------------------------------------
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const PersonSchema = new Schema({
  name: String,
  band: String,
  father: String
});

const ManagerSchema = new Schema({
  name: String,
  country: String
});

const BandSchema = new Schema({
  name: String
});

BandSchema.virtual("members", {
  ref: "Person", // The model to use
  localField: "name", // Find people where `localField`
  foreignField: "band", // is equal to `foreignField`
  // If `justOne` is true, 'members' will be a single doc as opposed to
  // an array. `justOne` is false by default.
  justOne: false,
  options: { sort: { name: -1 }, limit: 5 } 
});

BandSchema.virtual("managers", {
  ref: "Manager", // The model to use
  localField: "name", // Find people where `localField`
  foreignField: "country", // is equal to `foreignField`
  // If `justOne` is true, 'members' will be a single doc as opposed to
  // an array. `justOne` is false by default.
  justOne: false,
  options: { sort: { name: 1 }, limit: 5 } 
});

//BandSchema.set("toObject", { virtuals: true });
BandSchema.set("toJSON", { virtuals: true });

const Person = mongoose.model("Person", PersonSchema);
const Manager = mongoose.model("Manager", ManagerSchema);

const Band = mongoose.model("Band", BandSchema);

/**
 * Suppose you have 2 bands: "Guns N' Roses" and "Motley Crue"
 * And 4 people: "Axl Rose" and "Slash" with "Guns N' Roses", and
 * "Vince Neil" and "Nikki Sixx" with "Motley Crue"
 */
// Person.create([
//   {
//     name: "Axl Rose",
//     band: "Guns N' Roses"
//   },
//   {
//     name: "Slash",
//     band: "Guns N' Roses"
//   },
//   {
//     name: "Vince Neil",
//     band: "Motley Crue"
//   },
//   {
//     name: "Nikki Sixx",
//     band: "Motley Crue"
//   }
// ]);

// Manager.create([
//   {
//     name: "Bibi",
//     country: "South Africa"
//   },
//   {
//     name: "Storm",
//     country: "Italy"
//   },
//   {
//     name: "Wolverine",
//     country: "Canada"
//   },
//   {
//     name: "Jorge Pires",
//     country: "Brazil"
//   }
// ]);

// Band.create([{ name: "Motley Crue" }, { name: "Guns N' Roses" }]);
/////////////////////////////////////////////////////////////////////////

const app = require("express")();

app.use("/", (req, res) => {
  Band.find({})
    .populate("members")
    .populate("managers")
    .exec(function(error, bands) {
      /* `bands.members` is now an array of instances of `Person` */
      console.log(bands);
      res.json(bands);
    });
});

app.listen(3000, () => {
  console.log("We are on port 3000");
});

/**
 *
 
 */

考虑相关问题:

我的问题是:你如何定义foreignField

Members 正确填充,但 经理没有

我知道问题出在 foreignField 因为如果我重复成员的所有信息,它会正确填充,但现在我们有成员并使用相同的数据源进行管理。

经过一些研究,尝试在 Stack Overflow 上回答另一个问题 ( ), 我明白了它是如何工作的!

考虑部分代码:

BandSchema.virtual("members", {
  ref: "Person", // The model to use
  localField: "name", // Find people where `localField`
  foreignField: "band", // this field here has to match the ref path we want to populate!      
  justOne: false,

});

所以,诀窍是确保 foreignField 匹配 ref 模型中的字段,而 localField 是您可以在填充模型中找到的字段名称:我们必须在 foreignFieldlocalField 之间匹配,更准确地说:localField 的值必须与数据库中实际情况下的 foreignField 匹配,而不是在架构命名过程中。这就是 mongoose 查找和填充的方式!

现在我意识到我遇到的困难是 Virtual 以某种方式与 populate 相反的方向运行。您可以通过像树一样来填充:它只是从 id 填充到文档,而 virtual 将填充不包含密钥的文档,包含密钥的文档就是那个在填充过程中添加:它有点落后,我很难理解!要填充的文档没有刚刚填充的字段!这是令人兴奋的!

结论和最后的评论

尽管如此,与 populate 相比,我仍然觉得它有局限性。您仍然必须保留本地关键轨道,这不能解决例如故事记忆问题,而且根据我的研究,它的局限性更大。我看到的唯一优点是您不需要使用 _id,您可以使用任何您喜欢的键。我希望在这里 解决我的问题,但由于我们仍然必须存储本地密钥,我陷入了同样的陷阱!

更正

我很高兴地说我错了!虚拟真棒!并在 上解决了我的问题,我将在那里更新!