这也许更像是一个寻求建议的案例。但是,我将提供示例代码作为我想要实现的目标的示例。我试图建立我的第一个后端系统,我不断遇到问题的设计。
我的MongoDB数据库由四个主要部分组成-概要、团队、草稿和用户。概要文件(是使用ID获取所有信息的主要数据)模式具有与团队一起保存数组并起草ID的属性。其思想是,当配置文件被提供时,它将使用ID将所有这些属性填充到相关数据。
使用Mongoose的配置文件模式示例:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserProfileSchema = new Schema({
Name : String,
Email : String,
Admin : Boolean,
Status : Number,
UserID : String,
PrivateProfile: Boolean,
Drafts: [String], //This array holds the draft IDs
Teams:[String] //Holds the team profiles IDs
});
module.exports = mongoose.model('UserProfile', UserProfileSchema);使用Mongoose的示例:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeamProfileSchema = new Schema({
Name : String,
CaptainID : String,
CaptainName : String,
Members : [String], //Array of user IDs
DateCreated : Boolean,
Reputation : Number
});
module.exports = mongoose.model('TeamProfile', TeamProfileSchema);查找所有团队配置文件的路由示例,用户是该团队的队长,并获取与该团队关联的所有成员:
router.route('/teams/captain/:user_id')
.get(function (req, res) {
TeamProfile.find({
CaptainID : req.params.user_id
}, function (err, teams) {
if (err)
res.send(err);
for (var x in teams) {
var membersArray = [];
for (var i in teams[x].Members) {
var ID = teams[x].Members[i];
UserProfile.find({
UserID : ID
}, function (err, profile) {
if (err)
res.send(err);
membersArray.push(profile);
});
}
teams[x].Members = membersArray;
console.log(teams[x].Members);
}
res.json(teams);
});
})我知道这条路行不通,但我该如何做到这一点呢?我用了一种更普通的方法来解释我想要达到的目标。任何帮助都将不胜感激。
发布于 2016-06-15 07:47:06
我建议你使用denormalization和normalization.的组合
因为MongoDB不是关系数据库。
denormalization比normalization.快得多你不用再用恋爱了。
你可以读到这个文章希望可能对你有帮助。
干杯
https://stackoverflow.com/questions/37828930
复制相似问题