【问题标题】:Using Multiple schema for single Collection in Mongodb not Working在 Mongodb 中对单个集合使用多个模式不起作用
【发布时间】:2018-05-30 11:52:40
【问题描述】:

我想在 mongodb 中为单个集合使用多个模式,但除了单个模式之外它不存储。其他模式只存储 _id & _v 我的代码有什么问题? 我试图跟随 http://mongoosejs.com/docs/api.html#index_Mongoose-model

我的模式模型

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;

主要问题是它只从一个模式保存到数据库,而其他两个模式只保存_id、日期和_v

【问题讨论】:

    标签: node.js mongodb model schema


    【解决方案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;
    

    【讨论】:

    • 感谢您的最佳尝试,投票支持它工作正常但实际上上面的代码创建了 3 个我不想要的不同集合,我想将所有这些数据存储到单个集合中......无论如何我解决它..再次感谢您。
    【解决方案2】:
    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');
    

    【讨论】:

    • 内容不是构造函数,测验不是构造函数,这些错误来自路由。
    猜你喜欢
    • 2013-01-05
    • 2022-11-12
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多