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