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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Mongodb中使用多个模式进行单个集合无法正常工作相关的知识,希望对你有一定的参考价值。

我想在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

答案
    /* ---------------------  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;
另一答案
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');

以上是关于在Mongodb中使用多个模式进行单个集合无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

如何从 MongoDB 中具有多个集合的数据库中访问单个集合数据

单个 MongoDB 查询中的多个操作

在mongodb中,我们可以使用一个命令下载多个集合

跨多个集合的 Mongodb 批量插入

MongoDB数据建模性能

MongoDB事务