【问题标题】:Possible to save mongoose large data into multiple files可以将猫鼬大数据保存到多个文件中
【发布时间】:2019-10-29 06:58:06
【问题描述】:

是否可以将猫鼬大数据存储到多个文档中但应该在一个集合下

例如

-->Collection --> 客户,它有基于 ObjectId 的文档。

所以我正在寻找的是可以将数据存储到多个文档中,但所有这些都应该链接在一起吗?我的问题是我的服务器“当客户提出非常大的请求并保存数据时抛出错误,所以我需要解决方案来存储大数据。

drives:[{
name:string,
driveID:string,
phases:[] // this contain sub arrays and can be upto 20k lines of JSON because it contains a questionnaire
}]

例如

"phases":[
{
"phase":"abc"
  "phase" : "driveOne",
   "text" : "Drive One",
    "textKey" : "",
    "index" : "1.0",
    "subjects":[
    "id:"1.1.1",
    "text":"test",
    "textKey":"",
]
},

// for demo purpose I am copying same data...
{
"phase":"abc"
  "phase" : "driveOne",
   "text" : "Drive One",
    "textKey" : "",
    "index" : "1.0",
    "subjects":[
    "id:"1.1.1",
    "text":"test",
    "textKey":"",
]
},
{
"phase":"abc"
  "phase" : "driveOne",
   "text" : "Drive One",
    "textKey" : "",
    "index" : "1.0",
    "subjects":[
    "id:"1.1.1",
    "text":"test",
    "textKey":"",
]
},
{
"phase":"abc"
  "phase" : "driveOne",
   "text" : "Drive One",
    "textKey" : "",
    "index" : "1.0",
    "subjects":[
    "id:"1.1.1",
    "text":"test",
    "textKey":"",
]
},

]

【问题讨论】:

  • 您的数据是否有_id 或者您不关心_id
  • 不在模型中,但是是的,我可以使用 _id 访问文档
  • 能否提供数据?那是大吗?我想看看结构。
  • Okkk 会尽快回复您。
  • 你得到什么错误?

标签: node.js mongodb mongoose


【解决方案1】:

MongoDb 可以处理大量请求。如果您的服务器具有高配置,例如 RAM>= 8GB,那么它可以正常工作。单个文档的问题是它最多存储 16 MB 数据。如果再次被执行,我们必须对其进行分片。简单的解决方案是将数据存储在多个文档中。如果您一次获取大量数据,则必须对其进行分页。对于总大小等计算,您可以使用聚合查询。我在单个集合中测试了 9,00,000 个文档。我的聚合查询在 300 毫秒内响应,而正常查找需要 2.20 到 3 分钟。

分页 e:g-

 req.body.pageNo = 1
 req.body.pageSize = 100

  .skip(req.body.pageSize * ((req.body.pageNo === 0 ? 1 :  req.body.pageNo) - 1))
  .limit(req.body.pageSize)

【讨论】:

    【解决方案2】:

    在这里,您可以在 for 循环中更新您的文档。

    在我的示例中,根据您的数据库更改必要的关键字:

    userController.signUp = async function(req,res){
        req.body.installation = [
        {
            "phase":"abc",
            "phase" : "driveOne",
            "text" : "Drive One",
            "textKey" : "",
            "index" : "1.0",
            "subjects":[{
            "id":"1.1.1",
            "text":"test",
            "textKey":"",
            }]
        },
    
    
        {
            "phase":"abc",
            "phase" : "driveOne",
            "text" : "Drive One",
            "textKey" : "",
            "index" : "1.0",
            "subjects":[{
            "id":"1.1.1",
            "text":"test",
            "textKey":"",
            }]
        },
        {
            "phase":"abc",
            "phase" : "driveOne",
            "text" : "Drive One",
            "textKey" : "",
            "index" : "1.0",
            "subjects":[{
            "id":"1.1.1",
            "text":"test",
            "textKey":"",
            }]
        },
        {
            "phase":"abc",
            "phase" : "driveOne",
            "text" : "Drive One",
            "textKey" : "",
            "index" : "1.0",
            "subjects":[{
            "id":"1.1.1",
            "text":"test",
            "textKey":"",
            }]
        },
    
        ]
        //Your DB name instead of userModel
        userModel.findOne({_id: "5d1d9691019db61515450574"})
        .exec(async (err , found)=>{
            if(err){
                res.send(err);
            }else{
                for(var i = 0; i< req.body.installation.length; i++){
                    var hey = req.body.installation[i];
                    found.installation[i] = req.body.installation[i];
                    console.log("=============================+>" ,found.installation)
                    await userModel.findOneAndUpdate({_id: found._id} , found, {upsert: true , new: true});
    
                }
                res.send(found);
            }
        });
    
    }
    

    我已经尝试使用 2000,000 条记录,但它没有给出错误。

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多