【问题标题】:Create mongodb collections of a JSON response key value创建 JSON 响应键值的 mongodb 集合
【发布时间】:2019-08-01 11:03:40
【问题描述】:

我是 MongoDb 和 NodeJS 的新手。我想创建一个表的集合。

这是我的 JSON 回复:-

{
    "_id": "5c85f83516f5ad7ef3e14",
    "companyName": "XYZ",
    "fullName": "ABC",
    "designation": "Software Engineer",
    "companyID": "14224",
}

在这里,我为特定的companyName 获得了一个独特的companyID。我想创建尽可能多的集合,因为会有唯一的companyID。如何通过NodeJS、ExpressJS代码实现?

编辑:- 控制器文件。

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;

//----creating hash token for email verification ------------
const secret = crypto.randomBytes(20);
const hash = crypto.createHmac('sha256', secret)
                   .update(secret + admin.email)
                   .digest('hex');

//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;

    //saving users data to database
    admin.save((err, doc) =>{
        if(!err){
            res.send(doc);
                let coSchema = coName => {
                    return {
                        id: {type: String, index: true},
                        coName: {default: coName}
                    }
                }

                response.then((data) => {
                    let coSchema;
                    data.forEach((doc) => {
                        coSchema = coSchema(doc.companyID);
                        mongoose.model(doc.companyID, coSchema);
                    })
                }
        }
        else{
            if (err.code == 11000)
                    res.status(422).send(["Entered duplicate email address. Please check"]);
            else
            return next(err);
            }
    });
}

型号:=

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."
                },
designation :   {
                type: String,
                required: "Designation can't be empty."
                }           
});

mongoose.model('Admin', adminSchema);

【问题讨论】:

  • 我想创建尽可能多的集合,因为会有唯一的 companyID 你的意思是文档对吗?不收藏?
  • @GeorgeBailey 不,不是文件。我需要创建集合。在 DBS 中,我需要创建具有 companyIDs 值的集合,即; 14224 , 34344 ,54454 等
  • 所以您正在开发一个多租户应用程序?每个客户都有多个集合?检查这个stackoverflow.com/questions/15306916/…
  • @AZ_ 是的。你是对的
  • @AZ_ 没有多大帮助。

标签: node.js json mongodb express mongoose


【解决方案1】:

在接受 com 名称并返回架构对象的函数中定义您的公司架构。

说响应是从数据库读取的文档数组,解析每个文档并根据公司名称创建一个集合,确保公司名称在您的响应架构中定义为唯一

let coSchema = coName => {
	return {
		id: {type: String, index: true}, //need to have index created on some unique value to get the schema created without documents.
		coName: {default: coName}
		//add your feilds
	}
}

response.then((data) => {
	let coSchema;
	data.forEach((doc) => {
		coSchema = coSchema(doc.companyID);
		mongoose.model(doc.companyID, coSchema);
	})
})
相应地处理你的承诺。

【讨论】:

  • 我应该在控制器或模型中尝试这个吗?因为我猜想在控制器保存功能中注册时需要传递该功能。
  • 取决于您是否在架构中还需要公司名称,如果不需要,您可以为所有租户创建默认模型。
  • 我已经在我的 EDIT 中添加了控制器。我做对了吗?当我通过let coSchema = admin.companyName 时显示错误Unexpected token .
  • 我首先想要空集合。因为我正在使用具有不同模式的那些集合
  • 我的代码只是实现你想要做的事情的一个例子,你不能简单地将它复制粘贴到你已经开发的代码中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多