这个问题已经被问过很多次了,但我仍然不太明白在 mongoose
n 中引用另一个对象内的对象的两种方式有什么区别,也没有什么是最佳实践。>
场景 1(我目前的情况)
Model has an array of PhotoCode objects
------PhotoCodeSchema.js------
const mongoose = require('mongoose')
const photoCodeSchema = new mongoose.Schema({
value: { type: String, trim: true, unique: true },
validity: {type: Date, default: null },
status: {
type: String,
enum: ['valid', 'expired', 'attributed'],
default: 'valid',
},
}, { strict: true, timestamps: true },)
module.exports = mongoose.model('PhotoCode', photoCodeSchema)
------ModelSchema.js------
const photoCode = "../schemas/PhotoCodeSchema";
const modelSchema = new mongoose.Schema(
{
photoCode: [photoCode],
rating: Number,
...omitted_code
},
{ strict: false, timestamps: true }
);
场景 2(我在 SO 中看到的)
------PhotoCodeSchema.js------
const mongoose = require('mongoose')
const photoCodeSchema = new mongoose.Schema({
value: { type: String, trim: true, unique: true },
validity: {type: Date, default: null },
status: {
type: String,
enum: ['valid', 'expired', 'attributed'],
default: 'valid',
},
}, { strict: true, timestamps: true },)
module.exports = mongoose.model('PhotoCode', photoCodeSchema)
const photoCode = "../schemas/PhotoCodeSchema";
------ModelSchema.js------
const modelSchema = new mongoose.Schema(
{
payments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'PhotoCode'
}],
rating: Number,
...omitted_code
},
{ strict: false, timestamps: true }
);
我正在尝试做的用例。
我正在尝试找到在两个模式之间建立这种关系的“最佳”方式。
谢谢