【发布时间】:2016-01-21 09:42:10
【问题描述】:
好的,我来自 SQL 背景,我刚刚开始使用 MongoDB 和 Mongoose。
我有两个简单的模型:
汽车:
let mongoose = require('mongoose');
let carsSchema = new mongoose.Schema({
brand: String,
specfifications: [String],
image: String
});
module.exports = mongoose.model('Car', carSchema);
司机
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let driverSchema = new mongoose.Schema({
name: String,
cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }]
});
module.exports = mongoose.model('Driver', driverSchema);
据我所知,这似乎是处理一对多关系的正确方法,在这种关系中,许多司机可以使用一辆车。
现在我需要添加一个名为Races 的类,它将用于保存特定汽车中特定驾驶员的结果。
在 SQL 中思考我会做这样的事情:
let mongoose = require('mongoose');
let raceSchema = new mongoose.Schema({
totaltime: String,
laptime: String,
driver: { type: Schema.Types.ObjectId, ref: 'Driver' },
car: { type: Schema.Types.ObjectId, ref: 'Car' }
});
module.exports = mongoose.model('Race', raceSchema);
您认为这是在 NoSql 中的正确方法,还是我将我的 SQL 思维方式强加到 NoSQL 中?
【问题讨论】: