0

I am passing schema from model to controller. I want to create a collections in MongoDB as from pa particular JSON response upon post.

model:-

const mongoose = require ('mongoose');
const bcryptt = require('bcryptjs');
const crypto = require('crypto');

var adminSchema = new mongoose.Schema({
    companyName : {
                type: String,
                required: "Company  name can't be empty.",
                required: false
                },  
    companyID:  {
                type: String,
                unique: true
                }
                },
    email :     {
                type: String,
                required: "Email can't be empty.",
                unique: true
                },
    password:   {
                type: String,
                required: "First name can't be empty."
                },
    fullName : {
                type: String,
                required: "First name can't be empty."
                }
});

mongoose.model('Admin', adminSchema);

So here I am using companyName to create compantID in controller:-

module.exports.registerAdmin = (req, res, next) =>{ 
    var admin = new Admin();
    admin.companyName = req.body.companyName;
    admin.email = req.body.email;
    admin.password = req.body.password;
    admin.fullName = req.body.fullName;


//comapny id generate
const reqq = crypto.createHash('md5').update(admin.companyName).digest('hex');
let valueNum = reqq.match(/\d/g).join("").toString().substring(0,6);

admin.companyID = valueNum;
console.log(valueNum);

    admin.save((err, doc) =>{
        if(!err){
            res.send(doc);
            //todo something here to generate collection from valueNum
        }
        else{
            if (err.code == 11000)
                    res.status(422).send(["Entered duplicate email address. Please check"]);
            else
            return next(err);
            }
    });
}

So I am getting companyID in valueNum. Upon admin.save I want to pass valueNum in some code to create new collection of whatever unique valueNum will be in the existing DB. If collection with a particular valueNum already existing in the DB , then skip.

These all will happen in same single url hit. Response I am getting:-

{
    "_id": "5c874d88497ed26f233ffad5",
    "companyName": "Meta",
    "email": "xyzt@test.com",
    "fullName": "Aron",
    "companyID": "14624",

}

How can I achieve this ??

NoobCoder
  • 493
  • 8
  • 25
  • Im not getting what you are trying to say here. Do you want to create a new separate document to save the values of valueNum or else do you need to create a admin document with unique valueNum? – TRomesh Mar 12 '19 at 07:43
  • @TRomesh No, simply I want to create collections in my existing DB with the collection names of `valueNum` Ex: I have `valueNum` data as : "34500". Then 34500 name collection must get created and it goes on. But if already 34500 name collection is found then skip the function. – NoobCoder Mar 12 '19 at 08:08

2 Answers2

0

Are you trying to create Collections or new separate Documents? I'm not really understanding your question.

It would make a lot of sense if you were trying to create a new Document.

Quince Ngomane
  • 340
  • 1
  • 3
  • 16
  • I want to create new collections within a database . The collection names will be the value of `valueNum` which I am getting. – NoobCoder Mar 12 '19 at 08:49
0

Please refer to this post, I think it might help with your problem.

Dynamically create collection with Mongoose

Quince Ngomane
  • 340
  • 1
  • 3
  • 16