2017-12-16 15 views
1

mongodbの1つのコレクションに複数のスキーマを使用したいが、単一のスキーマ以外は格納しない。他のスキーマのみストア_id & _v私のコードで何が間違っていますか?私は http://mongoosejs.com/docs/api.html#index_Mongoose-modelMongodbの単一のコレクションに複数のスキーマを使用することはできません

マイスキーマモデル

const mongoose = require('mongoose'); 
const config = require('../config/database');  
var Schema = mongoose.Schema; 

// Reading content Schema 
const contentArticle = new Schema({ 
    contentCategory: { type: String }, 
    contentTitle: { type: String }, 
    body: { type: String }, 
});  

// Quiz Content 
const quizAllArticle = new Schema({ 
    quizCategory: { type: String }, 
    question: { type: String }, 
    ans: { type: String } 
});  

// Article Schema 
const topicSchema = new Schema({ 
    artId: { type: String }, 
    postURL: { type: String }, 
    date: { type: Date, default: Date.now }, 
    author: { type: String }, 
    published: {type: String }, 
    category: { type: String }, 
    QuesPapType :{ type: String }, 
}); 

const Content = module.exports = mongoose.model('Contents', contentArticle, 'articles'); 
const Quiz = module.exports = mongoose.model('Quizs', quizAllArticle, 'articles'); 
const Topic = module.exports = mongoose.model('TopicContent', topicSchema, 'articles'); 

や他のコードに従うことをしようとした は次のとおりです。

const express = require('express'); 
const router = express.Router(); 
const passport = require('passport'); 
const config = require('../config/database'); 
const Content = require('../models/art'); 
const Quiz = require('../models/art'); 
const Topic = require('../models/art'); 


router.post('/register',(req, res, next) => { 

    let topic= new Topic({  
     artId : req.body.artId, 
     author : req.body.author, 
     date : req.body.date, 
     published : req.body.published, 
     postURL : req.body.postURL, 
     category : req.body.pscCategory, 
     QuesPapType : req.body.QuesPapType,  
    }); 
    console.log(topic); 
    topic.save(function(err) { 
     if (err) { 
      res.json({ 
       success: false, 
       msg: 'failed to register' 
      }) 
     } else { 
      res.json({ 
       success: true, 
       msg: 'success to reg article' 
      }) 
     } 
    }) 
}); 

router.post('/reg-content', (req,res,next) => { 
    let content= new Content({  
     contentCategory : req.body.contentCategory, 
     contentTitle : req.body.contentTitle, 
     body : req.body.body,  
    }); 
    console.log(content);  
    content(function(err) { 
     if (err) { 
      res.json({success: false, msg: 'failed to register Content'}) 
     } else { 
      res.json({success: true,msg: 'success to reg article'}) 
     } 
    }) 
}) 

router.post('/reg-quiz', (req,res,next) => { 
    let quiz= new Quiz({  
     quizCategory : req.body.quizCategory, 
     question : req.body.question, 
     ans : req.body.ans,  
    }); 
    console.log(quiz); 
    quiz.save(function(err) { 
     if (err) { 
      res.json({success: false,msg: 'failed to register Content'}) 
     } else { 
      res.json({ success: true, msg: 'success to reg article'}) 
     } 
    }) 
})  

module.exports = router; 

主な問題は、それが唯一のスキーマからデータベースに保存していることで、他の2つのスキーマは_id、日付、_vのみ保存する

enter image description here

答えて

0
const Content = mongoose.model('Contents', contentArticle); 
const Quiz = mongoose.model('Quizs', quizAllArticle); 
const Topic = mongoose.model('TopicContent', topicSchema); 
module.exports = {Content, Quiz, Topic}; 


//import 
const {Content, Quiz, Topic} = require('your schema file'); 
+0

コンテンツは、コンストラクタではない、クイズは、これらのエラーはルートから来ているのコンストラクタではありません。 – Asutosh

1
/* --------------------- models/art.js ------------------------------------*/ 
    const mongoose = require('mongoose'); 
    var Schema = mongoose.Schema; 

    // Reading content Schema 
    const contentArticle = new Schema({ 
     contentCategory: { type: String }, 
     contentTitle: { type: String }, 
     body: { type: String }, 
    });  

    // Quiz Content 
    const quizAllArticle = new Schema({ 
     quizCategory: { type: String }, 
     question: { type: String }, 
     ans: { type: String } 
    });  

    // Article Schema 
    const topicSchema = new Schema({ 
     artId: { type: String }, 
     postURL: { type: String }, 
     date: { type: Date, default: Date.now }, 
     author: { type: String }, 
     published: {type: String }, 
     category: { type: String }, 
     QuesPapType :{ type: String }, 
    }); 



    const Content = mongoose.model('Contents', contentArticle); 
    const Quiz = mongoose.model('Quizs', quizAllArticle); 
    const Topic = mongoose.model('TopicContent', topicSchema); 


    module.exports = {Content, Quiz, Topic}; 

    /*--------------------routes/index.js ------------------------------------*/ 

    var express = require('express'); 
    var router = express.Router(); 
    const {Content, Quiz, Topic} = require('../models/art.js'); 

    /* GET home page. */ 
    router.get('/', function(req, res, next) { 
     res.render('index', { title: 'Express' }); 
    }); 




    router.post('/register',function(req, res, next) { 

     let topic= new Topic({  
      artId : req.body.artId, 
      author : req.body.author, 
      date : req.body.date, 
      published : req.body.published, 
      postURL : req.body.postURL, 
      category : req.body.pscCategory, 
      QuesPapType : req.body.QuesPapType,  
     }); 
     console.log(topic); 
     topic.save(function(err) { 
      if (err) { 
       res.json({ 
        success: false, 
        msg: 'failed to register' 
       }) 
      } else { 
       res.json({ 
        success: true, 
        msg: 'success to reg article' 
       }) 
      } 
     }); 
    }); 

    router.post('/reg-content', function(req,res,next){ 
     let content= new Content({  
      contentCategory : req.body.contentCategory, 
      contentTitle : req.body.contentTitle, 
      body : req.body.body,  
     }); 
     console.log(content);  
     content(function(err) { 
      if (err) { 
       res.json({success: false, msg: 'failed to register Content'}) 
      } else { 
       res.json({success: true,msg: 'success to reg article'}) 
      } 
     }) 
    }); 

    router.post('/reg-quiz', function(req,res,next){ 
     let quiz= new Quiz({  
      quizCategory : req.body.quizCategory, 
      question : req.body.question, 
      ans : req.body.ans,  
     }); 
     console.log(quiz); 
     quiz.save(function(err) { 
      if (err) { 
       res.json({success: false,msg: 'failed to register Content'}) 
      } else { 
       res.json({ success: true, msg: 'success to reg article'}) 
      } 
     }) 
    });  

    module.exports = router; 
+0

最高の努力をいただきありがとうございます、それはうまく動作しますが、実際にコードの上に私が望んでいない3つの異なるコレクションを作成する、私はこれらのすべてのデータを単一のコレクションに格納したい..とにかく私はそれを解決..君は。 – Asutosh

関連する問題