我应该如何使用 MEAN 堆栈来处理我的后端设计?

How should I approach my back end design using the MEAN stack?

这可能更像是一种寻求建议的情况。但是,我将提供示例代码作为我想要实现的示例。我正在尝试构建我的第一个后端系统,但我一直 运行 遇到设计问题。

我的 MongoDB 数据库由 4 个主要部分组成 - 配置文件、团队、草稿和用户。配置文件(是使用 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);
});
})

我知道这条路线行不通,但我该怎么做呢?我使用更普通的方法只是为了解释我想要实现的目标。任何帮助将不胜感激。

我建议您结合使用 非规范化规范化

因为MongoDB不是关系数据库

反规范化规范化快得多。你不需要再使用关系了。

你可以看看这个article希望对你有帮助。

干杯