谁能提供例子,如何保存猫鼬虚拟化数据库关联使用?
模型中已经有这样的内容:
const userSchema = new Schema({
googleId: String,
name: String,
email: String,
password: String,
credits: {type: Number, default: 0},
});
userSchema.virtual('advertisements', {
ref: 'advertisements',
localField: '_id',
foreignField: '_id',
justOne: false,
options: { sort: { name: -1 }, limit: 5 }
});
mongoose.model('users', userSchema);在文档中有关于数据填充和检索的信息,但是应该如何正确保存呢?
一定要是user.advertisements.localfield = localfield之类的吗?或者它是如何工作的?
发布于 2019-06-10 12:01:58
虚拟化是您可以获得和设置的文档属性,但不会持久化到MongoDB。getters用于格式化或组合字段,而setter则用于将单个值分解为多个值以进行存储。
这意味着我们只能使用虚拟文件进行数据准备,例如,我们可以合并first name和last name,或者使用引用填充文档。
发布于 2019-06-10 11:55:18
猫鼬支持虚拟属性。虚拟属性是便于使用的属性,但不会持久化到mongodb中。
您不能保存虚拟数据,它是唯一的getter。
https://stackoverflow.com/questions/56525715
复制相似问题