请考虑以下代码:
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");
});
/**
*https://stackoverflow.com/questions/43882577/mongoosejs-virtual-populate
https://stackoverflow.com/questions/60875380/populate-virtuals-does-not-seem-to-work-could-anyone-show-me-the-error
*/
考虑相关问题:
我的问题是:您如何定义foreignField
?
Members
正确填充,但manager not。
我知道问题是foreignField
,因为如果我重复来自成员的所有信息,它将正确填充,但是现在我们有了成员,并使用相同的数据源进行管理。
[经过一些研究,尝试在Stack Overflow(mongoose: populate in mongoose which doesn't have any ObjectId),我意识到它是如何工作的!
考虑提供的代码部分:
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
是您在填充模型中可以找到的字段的名称:我们必须在[C0 ]和foreignField
,更准确地说:localField
的值必须在实际情况下(在数据库中而不是在模式命名过程中)与localField
之一匹配。那是猫鼬可以找到并居住的方式!
[现在我意识到我遇到的困难是foreignField
沿Virtual
的相反方向运行。您可以通过像树一样填充图片:它只是从populate
填充到文档中,而id
将填充不包含键的文档,包含键的是要添加的文档。填充过程:某种程度上是落后的,这让我很难理解!要填充的文档没有刚刚填充的字段!真是令人赞叹!
结论和最后发言
尽管如此,与virtual
相比,我仍然发现它受到限制。您仍然必须保持本地键跟踪,这不能解决例如故事记忆问题,并且根据我的研究,它的局限性更大。我看到的唯一好处是您不需要使用populate
,您可以使用任何键。我原本希望用它来解决我的问题_id
,但是由于我们仍然必须存储本地密钥,因此我陷入了同样的陷阱!
更正
[我很高兴地说我错了!虚拟效果太棒了!并解决我对How to save an JSON file using GridFs的问题,我将在那里更新!