2013-05-20 7 views
-2

안녕하세요 저는 NodeJS와 MongoDB로 phorum을 구축하고 있습니다. 정말이 tecnologies와 새로운 임, 나는 몇 가지 질문이 있습니다. 내가 buid이 내 마음에 현재 스키마가 있습니다. 이 답변이 주제를 가지고 범주가 지역,,, 위치 :몽구스 + NodeJS 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 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 
]; 

가 embedings 문서가 있습니다, 그리고 코드에서 나는 같은 것을 넣어 :

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에 찾아 가서?

아마 내 스키마가 잘못되었습니다. 감사합니다.

답변

-1

그리고이 somearea의 일부 categorie 몇 가지 주제에 대한 답을 추가하기위한 하나의 컨트롤러, 미친 것 :

// 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); 

        }); 

       } 

      }); 
     }); 

    }); 

};