在Mongodb中使用多个模式进行单个集合无法正常工作

问题描述 投票:1回答:2

我想在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,date和_v

enter image description here

node.js mongodb model schema
2个回答
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
投票
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');
© www.soinside.com 2019 - 2024. All rights reserved.