Home > Back-end >  In Mongo, If a document I'm saving "Prateek" then I don't want on the next creat
In Mongo, If a document I'm saving "Prateek" then I don't want on the next creat

Time:05-07

//** If I'm adding a new document with the name: "India", then I don't want that the DB allow another name with the name: "INDIA", "india", "indIA", etc. I'm new and learning, help would be great!!**

// Controller

var Dinosaur = require('../models/dinosaurs');

//addDino
module.exports.addDino = (req, res) => {
    var name = req.body.name;
    var type = req.body.type;
    var height = req.body.height;
    var weight = req.body.weight;
    var Period = req.body.Period;

    req.checkBody('name', 'Name is required').notEmpty();

    var errors = req.validationErrors();
    if (errors)
        return res.status(400).send({
            message: 'Name is Required'
        });
    else {
        let newDino = {
            name: name,
            type: type,
            height: height,
            weight: weight,
            Period: Period
        }
        Dinosaur.addDino(newDino, (err, result) => {
            if (err) {
                if (err.name) return res.status(409).send({
                    message: name   ' Already Exist'
                });
                else if (err.url) return res.json({ status: false, error: { url: "Url already exist" }, message: err.url });
                else return res.json(err, "Server Error");
            }
            else {
                return res.status(200).send({
                    message: "Done"
                });
            }
        });
    }
}

// Model

var mongoose = require('mongoose');

//dinosaur schema
var DinosaurSchema = mongoose.Schema({
    name: {
        type: String,
        unique: true
    },
    type: {
        type: String
    },
    height: {
        type: Number
    },
    weight: {
        type: Number
    },
    Period: {
        type: String
    }
});

var Dinosaur = mongoose.model('dinosaur', DinosaurSchema);

//add
module.exports.addDino = (query, callback) => {
    Dinosaur.create(query, callback);
}

// GetAll, Already Created a new document with the name "Brachiosaurus"

enter image description here

// > Create, a new create with the first letter lower case "brachiosaurus", Don't want it to be pushed. enter image description here

//Get All, Got pushed. enter image description here

CodePudding user response:

You can create an index on name and when you are saving it makes it lower case. So, every time you save it you are following the same convention. It will be easy to show name on UI as you want.

db.collection.createIndex({
    "name": 1
}, {
   unique: true
})

Note: adding an index might impact the insertion and update performance.

Make these changes to your controller:

var Dinosaur = require('../models/dinosaurs');

//addDino
module.exports.addDino = (req, res) => {
    var name = req.body.name;
    var type = req.body.type;
    var height = req.body.height;
    var weight = req.body.weight;
    var Period = req.body.Period;

    req.checkBody('name', 'Name is required').notEmpty();

    var errors = req.validationErrors();
    if (errors)
        return res.status(400).send({
            message: 'Name is Required'
        });
    else {
        let newDino = {
            name: name.toLowerCase(), // make this changes
            type: type,
            height: height,
            weight: weight,
            Period: Period
        }
        Dinosaur.addDino(newDino, (err, result) => {
            if (err) {
                if (err.name) return res.status(409).send({
                    message: name   ' Already Exist'
                });
                else if (err.url) return res.json({ status: false, error: { url: "Url already exist" }, message: err.url });
                else return res.json(err, "Server Error");
            }
            else {
                return res.status(200).send({
                    message: "Done"
                });
            }
        });
    }
}
  • Related