在mongodb中存储数据的正确方法

时间:2018-01-16 18:24:23

标签: node.js mongodb

存储数据的最佳方式是什么?就像我的项目有companybrand集合,而Schema就像

一样

company集合

{
company_name : X,
brand_id : [1,2] // here I store the id's of brands that has same company 
}

brand集合

{
brand_name : y,
price : 10;
}

因此,为了保存同一公司的数据,我使用步骤 -

1 - 在brand_name集合中插入pricebrand

2 - 在公司集合中插入company_name并从id中获取brand_id,从brand_collection获取结果。

EX -

         brand.insert({brand_name : y,price : 10},function(err,brand_result){

                 company.insert({company_name : X},function(error,company_result){
                          company.update({company_name : X},{
                               $push : {
                                 brand_id : brand_result.id
                                 }
                          },function(errs,result){
                              res.send({message : 'Data save'});
                           });  
                       });
                  });

我不知道如何,但根据我的说法,这不是一个好方法,因为 首先,这需要花费大量时间插入数据

和第二当我从 CSV文件插入数据时,我无法在同一公司的不同品牌的brand_id更新company collection

所以请给我一些建议并提出一些更好的方法。

0 个答案:

没有答案
相关问题