您好我正在使用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);” mongoose构建一个集合。我只想要一个集合,因为我读到它更好,更高效。
如果我想要一个带有大JSON对象的集合,我怎么能阅读一些主题?像这样:
AreaModel.find({'categories.topics.id': 'xxx'}, function(){})
但是,它的posible在主题id中找不到类别id?
也许我的架构错了。 谢谢大家。
答案 0 :(得分:-1)
这是一个控制器,用于在somearea的某些类别的某个主题中添加答案,看起来很疯狂:
// 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);
});
}
});
});
});
};