问题
I am studying the Populate Virtuals: https://mongoosejs.com/docs/populate.html#populate-virtuals
require("./connection");
// //----------------------------------------------------
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
name: String,
band: 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 }
});
const Person = mongoose.model("Person", PersonSchema);
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"
// }
// ]);
// Band.create([{ name: "Motley Crue" }, { name: "Guns N' Roses" }]);
/////////////////////////////////////////////////////////////////////////
Band.find({})
.populate("members")
.exec(function(error, bands) {
/* `bands.members` is now an array of instances of `Person` */
console.log(bands.members);
});
However, the output of this code is undefined
; the mongoose tutorial claims it should be "an array of instances of Person
".
I have tested another code, but I have gotten similar results: http://thecodebarbarian.com/mongoose-virtual-populate.html
First: could anyone let me know what is wrong on this code? I cannot see it!
Second: if it is not asking too much, could anyone try to show me the importance of this technique. They claim it is better than conventional populate regarding speed, I cannot see it at my current mongoose knowledge!
Related question: Mongoosejs virtual populate
回答1:
The answer was right in front of my eyes! The question I have related to, namely, has the answer after all: Mongoosejs virtual populate
In order to keep the learning spirit, I shall summarize here the solution.
According to the official documentation:
If you use toJSON() or toObject() mongoose will not include virtuals by default.
See: https://mongoosejs.com/docs/guide.html#virtuals.
Honestly, I have no idea what that means, but it was included in a comment, and seems the answer!
So, what ones has to do is include the line:
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 }
});
//----------------------------------------------------------------
//here, this one!
BandSchema.set("toObject", { virtuals: true });
BandSchema.set("toJSON", { virtuals: true });
//------------------------------------------
I have been studying the mongoose official tutorial, "seriously?! guys, you can improve!" I have found it challenging.
来源:https://stackoverflow.com/questions/60875380/populate-virtuals-does-not-seem-to-work-could-anyone-show-me-the-error