mongodb - MongoDB:如何查找数据库中集合之间的关系
问题描述
我有一个集合,user
其中有id, firstName, lastName
. id
集合字段user
已在另一个集合中使用。
有没有办法找到所有使用用户 ID的集合?
用户架构:
let userSchema = new mongoose.Schema({
firstName: {
type: String,
trim: true,
required: true
},
lastName: {
type: String,
trim: true,
required: true
}
},
{
timestamps: true,
usePushEach: true
});
训练架构:
var trainingSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
name: {type: String, required: true},
institution: {
instituteName: {type: String, required: true},
type: {type: String, required: true},
address: {
country: String,
state: String,
city: String
}
},
startDate: Date,
endDate: Date,
achievements: String,
createdAt: Date,
updatedAt: Date,
endorsers: [{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
profilePic: {
container: {type: String,default: null},
name: { type: String, default: null }
},
currentPosition: {type: String,default: ""},
currentWorkingCompany: {type: String,default: ""}
}],
});
在上面的模式中userId来自用户集合
注意:与 MYSQL 中可用的类似:
SELECT
ke.referenced_table_name 'parent table',
ke.referenced_column_name 'parent column',
ke.table_name 'child table',
ke.column_name 'child column',
ke.constraint_name
FROM
information_schema.KEY_COLUMN_USAGE ke
WHERE
ke.referenced_table_name IS NOT NULL
AND table_schema = 'your_db_name'
ORDER BY ke.referenced_table_name;
来源:这里
解决方案
推荐阅读
- elixir - 将宏作为 Plug 选项传递无法编译
- c++ - 是否可以将 C++ UWP 应用程序链接到 Mysql 数据库?
- python - MongoDB Python 和 C++ 客户端 - 多个实例出错
- java - 如何将 CoyoteInputStream 转换为 FileInputStream?
- unit-testing - 通过 handle_info 对周期性计时器进行单元测试
- npm - 重新安装 Windows 后所有项目上的浏览器同步错误
- javascript - 从 html 菜单中读取值?
- rest - 通过 REST 将 msg 发送到 Azure 服务总线队列
- regex - 找出一个字符串是否只包含 ASCII 字符
- amazon-cloudfront - 在记录之前修改 CloudFront 请求?