-2

嗨,我正在用 NodeJS 和 MongoDB 构建一个 phorum。我对这种技术真的很陌生,我有一些问题。我有当前的模式来构建这个。有一些领域,有类别,有主题,有答案:

var Phorum = [
{
    position: 1,
    name: [
        {
            lang: 'es',
            value: 'Ideas'
        }
    ],
    categories: [
        {
            position: 1,
            name: [
                {
                    lang: 'es',
                    value: 'Interfaz visual'
                }
            ],
            topics: [
                {
                    createdAt: 'xxx',
                    updatedAt: 'xxx',
                    userId: 123,
                    status: 1,
                    spam: false,
                    views: 42,
                    likes: [321,231],
                    details: [
                        {
                            lang: 'es',
                            title: {
                                normal: 'Tengo una pregunta',
                                url: 'tengo-una-pregunta'
                            },
                            text: {
                                original: 'El texto...',
                                formated: 'El texto...' // Esta con los enlaces y todo reemplazado
                            },
                            tags: ['tag1', 'tag2']
                        }
                    ],
                    answers: [
                        {
                            createdAt: 'xxx',
                            updatedAt: 'xxx',
                            likes: [231, 321],
                            text: [
                                {
                                    lang: 'es',
                                    createdAt: 'xxx',
                                    original: 'Respuesta...',
                                    formated: 'Respuesta...' // Con enlaces, videos,... formateado
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
];

有一个嵌入文件,在代码中我放了这样的东西:

var mongoose = require('mongoose'),
    Schema = process.DB.Schema,
    ObjectId = process.DB.ObjectId;


/*
 * ESQUEMA DE LOS DETALLES DE UN TEXTO
 * Se usa en las respuestas y en los topics
 */
var textSchema = new Schema({
    createdAt: {type: String, default: Date.now},
    original: String,
    formated: String
});


/*
 * ESQUEMA DEL TEXTO DE UNA RESPUESTA
 */
var TextAnswerSchema = new Schema({
    lang: {type: String, required: true, trim: true, lowercase: true},
    text: [textSchema]
});

// Esquema de las respuestas
var AnswersSchema = new Schema({
    createdAt: {type: Date, default: Date.now},
    likes: {type: [ObjectId], ref: 'User', unique: true},
    text: [TextAnswerSchema]
});

// Esquema de los detalles de un topic
var DetailsTopicSchema = new Schema({
    lang: {type: String, required: true, trim: true, lowercase: true},
    title: {
        original: String,
        url: String
    },
    text: [textSchema],
    tags: {type: [String], unique: true, trim: true}
});

// Esquema para un topic
var TopicsSchema = new Schema({
    createdAt: {type: Date, default: Date.now},
    //userId: {type: ObjectId, ref: 'User'},
    userId: {type: Number},
    status: {type: Number, default: 1, min: 1, max: 3},
    spam: {type: Boolean, default: false},
    views: {type: Number, default: 0, min: 0},
    //likes: {type: [ObjectId], ref: 'User', unique: true},
    details: [DetailsTopicSchema],
    answers: [AnswersSchema]
});



// Detalles de los nombres de una categoria
var DetailsNameSchema = new Schema({
    lang: {type: String, required: true, trim: true, lowercase: true},
    value: {type: String, trim: true}
});

// Esquema de las categorias
var CategoriesSchema = new Schema({
    position: {type: Number, default: 9999},
    name: [DetailsNameSchema],
    topics: [TopicsSchema]
});

// La estructura de la coleccion tracker
var AreasSchema = new Schema({
    position: {type: Number, default: 9999},
    name: [DetailsNameSchema],
    categories: [CategoriesSchema]
});

// Exponemos los modelos
// Guardamos en los modelos de mongoose
mongoose.model('AreasSchema', AreasSchema);
mongoose.model('CategoriesSchema', CategoriesSchema);
mongoose.model('DetailsNameSchema', DetailsNameSchema);
mongoose.model('TopicsSchema', TopicsSchema);
mongoose.model('DetailsTopicSchema', DetailsTopicSchema);
mongoose.model('AnswersSchema', AnswersSchema);
mongoose.model('TextAnswerSchema', TextAnswerSchema);
mongoose.model('textSchema', textSchema);

但是当我使用“mongoose.model('abc', abc);”时 猫鼬建立一个集合。我只想要一个集合,因为我读到它更好,更有效。

如果我想要一个带有大 JSON 对象的集合,我如何阅读一些主题?像这样的东西:

AreaModel.find({'categories.topics.id': 'xxx'}, function(){})

但是,它可以在没有类别 id 的主题 id 中找到吗?

也许我的架构是错误的。谢谢大家。

4

1 回答 1

-1

这是一个控制器,用于在某些区域的某些类别的某些主题中添加答案,看起来很疯狂:

    // new answer in topic
    server.post('/api/v1/tracker/topics/:topicID', function(req, res, next){

        // To JSON
        var topicID = req.params.topicID,
            body = JSON.parse(req.body);

        // If not is array
        if(body.details && !__.isArray(body.details)){
            body.details = [body.details];
        }

        //Find topic
        AreaModel.findOne({'categories.topics._id': topicID}, 'categories.topics._id categories._id categories.topics.answers', function(err, doc){
        //AreaModel.findOne({'categories.topics._id': topicID}, function(err, doc){


            // Find the current topic
            __.find(doc.categories[0].topics, function(topic, key){ 

                // If find
                if(topic._id == topicID){

                    var TextAnswerSchema = mongoose.model('TextAnswerSchema'),
                        AnswersSchema = mongoose.model('AnswersSchema'),
                        arrayAnswers = [],
                        det;

                    // Each response can be in diferent langs
                    __.each(body.details, function(detail){

                        det = new TextAnswerSchema({
                            lang: detail.lang,
                            original: detail.value,
                            formated: detail.value
                        });
                        arrayAnswers.push(det);

                    });

                    // Answer
                    var answers = new AnswersSchema({
                        text: arrayAnswers
                    });

                    // Add to doc in correct place
                    doc.categories[0].topics[key].answers.push(answers);

                    // Save to mongodb
                    doc.save(function(err, doc){

                        if(err){
                            console.log(err);
                        }
                        res.send(doc);

                    });

                }

            });
        });

    });

};
于 2013-05-21T12:33:44.600 回答