我确信MongoDB没有正式支持"加入".这是什么意思?
这是否意味着"我们无法将两个集合(表格)连接在一起."?
我想如果我们把_id集合A 的值放到other_id集合B中,我们可以简单地连接两个集合吗?
如果我的理解是正确的,MongoDB可以将两个表连接在一起,比如说,当我们运行查询时.这是通过http://www.mongodb.org/display/DOCS/Schema+Design中的"参考"完成的.
那么"加入"究竟意味着什么呢?
我很想知道答案,因为这对学习MongoDB架构设计至关重要.http://www.mongodb.org/display/DOCS/Schema+Design
项目组架构:
var ProjectGroupSchema = new Schema({
projectGroupId : String,
title : String
});
Run Code Online (Sandbox Code Playgroud)
项目架构:
var ProjectSchema = new Schema({
title : {type : String, default : '', required : true},
group : {type: String, ref: 'ProjectGroup' },
subscribers : [{type: String, ref: 'User' }]
});
Run Code Online (Sandbox Code Playgroud)
用户架构:
var UserSchema = new Schema({
userId : {type: String, require: true},
firstName : {type: String, required: true},
lastName : {type: String, required: true},
});
Run Code Online (Sandbox Code Playgroud)
然后,我可以进行以下填充:
project.findById(req.projectId})
.populate('subscribers')
.populate('group')
.exec(function(err, …Run Code Online (Sandbox Code Playgroud) 我有 2 个集合,它们是一对多的关系。如何使用猫鼬获取相关数据作为嵌套文档?
我有 2 个模式,它们是这样相关的..
var userSchema = mongoose.Schema({
name : String,
age : Number,
});
var postSchema = mongoose.Schema({
title: String,
body: String,
user: {type: mongoose.Schema.Types.ObjectId, ref:'User'}
});
Run Code Online (Sandbox Code Playgroud)
并且mongodb中有数据..
//user:
{
_id:"5694934cab2816601db06291",
name:"John",
age:16
},
{
_id:"5694934cab2816601db06292",
name:"Kim",
age:61
}
//post :
{
_id:"569494e5ab2816601db06293",
title:"hi",
body:"nice to meet you",
user:"5694934cab2816601db06291"
},
{
_id:"569494e5ab2816601db06294",
title:"hello",
body:"how are you",
user:"5694934cab2816601db06292"
}
Run Code Online (Sandbox Code Playgroud)
使用猫鼬获得这样的结果的最佳方法是什么?
{
_id:"569494e5ab2816601db06293",
title:"hi",
body:"nice to meet you",
user:{
_id:"569494e5ab2816601db06294",
name:"John",
age:16
}
},
{
_id:"569494e5ab2816601db06294",
title:"hello",
body:"how are …Run Code Online (Sandbox Code Playgroud)