我有一个用户文档和一个事务文档。
我需要得到用户的交易,并通过一些属性进行过滤,比如,日期范围和支付或未支付。
由于我的交易Schema是无限制的,我使用mongoose virtuals来填充用户的交易,但是我看到它不可能对一个虚拟的交易进行MongoDb查询,因为它并不真正在数据库中。
的使用模式。
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
trim: true,
unique: true,
},
...
},
);
userSchema.virtual('transactions', {
ref: 'Transaction',
localField: '_id',
foreignField: 'owner',
});
事务模式。
const transactionSchema = new Schema({
amount: {
type: Number,
required: true,
},
...
owner: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
});
我觉得我这样建模数据库的时候犯了一些错误。这种情况下,哪种方法是最好的?