【发布时间】:2016-06-17 13:42:38
【问题描述】:
我正在尝试找出哪种方法更具可扩展性。 我有一个用户在拼车旅行中申请了座位,并且该用户需要能够查看适用于他们的所有旅行。我的模型如下所示:
var UserSchema = new mongoose.Schema({
id: String,
name: String,
trips: [String] // An array of strings, which holds the id of trips
});
var TripSchema = new mongoose.Schema({
id: String,
description: String,
passengers: [String] // An array of strings, which holds the id of users
});
因此,当用户查看适用于他们的所有行程时,我的后端将搜索 Mongo 数据库中的所有行程。 我正在选择两种方法:
- 搜索所有行程并返回用户 id 在乘客数组中的行程
- 搜索所有行程并返回 ID 与用户行程数组中的 id 匹配的行程。
我认为方法 #2 更好,因为它不必在 Trip 模型中进行更深入的搜索。我只是在寻求确认,想知道是否还有其他需要考虑的事情。
【问题讨论】:
标签: javascript mongodb mongoose nosql