Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - Is it efficient to store data in MongoDB by creating multiple collections from same schema?

I would like to create one schema, and make multiple models from this one schema.

Could someone please let me know if this is a good collection design for MongoDB? I would like to categorize my collections by similar data in each collection, to avoid unnecessary searches by object parameters. My application is designed in such a way, that it will only need the data from one collection, to represent it on the front end.

To illustrate the point, here is the example of the app collecting information about local Trash Pandas and other animals lurking in the shadows. Let's say we take cute pictures of them, scan their faces with LIDAR for recognition("LIDAR is dead!"), give them nicknames and store in a MongoDB. Now, instead of searching by types in one collection, I would like to load all of the objects from one collection on the page.

Is taking all of the objects in one collection more efficient than searching and narrowing down by type?

Route Server in Server.js

const router = express.Router();
const {Racoons} = require('../models/mammal');
const {Opossums} = require('../models/mammal');
const {Skunkies} = require('../models/mammal');



router.post("/support_your_local_street_cats", function (req, res) {

    if (req.body.TrashPandaType === "Racoon"){
        const newTag = new Racoons({
            TrashPandaType:     req.body.TrashPandaType,
            nameTag:            req.body.nameTag,
            opposableThumbs:    req.body.opposibleThumbs,
            sweetSmile:         req.body.sweetSmile,
            sizeKg:             req.body.sizeKg,
            trashFoodAllergies: req.body.trashFoodAllergies
            });
    
    
    
    }elseif (req.body.TrashPandaType === "Opossum"){
        const newTag = new Opossums({
            TrashPandaType:     req.body.TrashPandaType,
            nameTag:            req.body.nameTag,
            opposableThumbs:    req.body.opposibleThumbs,
            sweetSmile:         req.body.sweetSmile,
            sizeKg:             req.body.sizeKg,
            trashFoodAllergies: req.body.trashFoodAllergies
            });
    
    
    }elseif (req.body.TrashPandaType === "Skunk"){
        const newTag = new Skunkies({
            TrashPandaType:     req.body.TrashPandaType,
            nameTag:            req.body.nameTag,
            opposableThumbs:    req.body.opposibleThumbs,
            sweetSmile:         req.body.sweetSmile,
            sizeKg:             req.body.sizeKg,
            trashFoodAllergies: req.body.trashFoodAllergies
            });
    }
    
    

    newRequest
        .save()
        .then(result => {
            console.log(result)
            res.json({ state: true, msg: "TrashPanda tagged successfully..!" });
        })
        .catch(error => {
            console.log(error)
            res.json({ state: false, msg: "TrashPanda tagged unsuccessfully..!" });
        })
});

Models in mammal.js

const mongoose = require('mongoose');
const?trashPanda?=?mongoose.Schema({

????ObjectProp:?{
        TrashPandaType:     String,
        nameTag:        ??  String,
        opposableThumbs:    String,
        sweetSmile:         String.
        sizeKg:??           Number,
        trashFoodAllergies:?String
??????}
    }
    );
    

module.exports?=?mongoose.model('Racoons', trashPanda, 'Racoons');
module.exports?=?mongoose.model('Opossums', trashPanda, 'opossums');
module.exports?=?mongoose.model('Skunkies', trashPanda, 'Skunkies');
question from:https://stackoverflow.com/questions/65896536/is-it-efficient-to-store-data-in-mongodb-by-creating-multiple-collections-from-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...