Answer the question
In order to leave comments, you need to log in
How to create a virtual field in Mongoose?
hello world! I have a User model, it is necessary to create a virtual field in it, which will receive the referrals of this user (those with referalUserName = userName), how to implement this? It's not clear from the Mongoose documentation :)
const mongoose = require('mongoose'),
Schema = mongoose.Schema;
const userSchema = new Schema({
name: String,
password: String,
email: String,
userName: String,
referalUserName: String,
referalId: String
});
userSchema.virtual('refs', {
ref: 'User',
localField: 'referalUserName',
foreignField: 'userName'
});
const User = mongoose.model("User", userSchema);
User.find({}).populate('refs').exec(function(error, referalList) {
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question