【问题标题】:Possible to save Mongo data into multiple documents可以将 Mongo 数据保存到多个文档中
【发布时间】:2019-11-06 18:25:20
【问题描述】:

我的问题是是否可以将 Mongo 数据保存到多个文档中并将它们链接在一起?我有非常大的 json 文件,所以我想将它们拆分为多个文件并以这种方式保存它们,每个文档文件的大小会更小吗?

这是我的代码,它创建了非常大的 JSON 文件和子数组,我想让每个文档的大小尽可能小。

 router.post("/request/partial", async (req, res) => {
      const partial = new PartialRequest({
        startingDate: req.body.startingDate,
        requestID: req.body.requestID,
        whoCreated: req.body.whoCreated,
        isNewRequest: true,
        specialRequest: req.body.specialRequest,
        contactInformation: req.body.contactInformation,
        requestInformation: req.body.requestInformation,
        orderInformation: req.body.orderInformation,
        jobSiteInformation: req.body.jobSiteInformation,
 installations : req.body.installations;
      });
    await partial.save().then(result => {
        return res.status(200).json({
          message: 'Handling POST request to /partial',
          result: result
        });
      });
      await Request.findOneAndRemove({
        requestID: req.body.requestID
      }).then(data => {
        return res.status(200).json(data);
      }).catch(error => {
        return res.status(500).json({
          error: error
        })
      })
    })

【问题讨论】:

  • 您希望您的所有数据都存储在单个 model 中,但要逐步进行。对吧?
  • 可能是的,更好的解决方案是什么?我应该拆分模型吗?我只想在每个 mongo 文档中存储尽可能小的数据..
  • 您可以将数据存储在多个文档中的单个集合中。这样会更好。

标签: node.js mongodb mongoose


【解决方案1】:

在这里您可以在 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);
        }
    });

}

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

【讨论】:

  • 嗨,我已经修改了部分代码,以便您可以理解...因为我有安装数组,但是当我运行此函数时,它根本不存储安装,并且在文档安装中:[ ] 只是空数组
  • 已更新。对不起那个错误。 @MuhammadAli
  • 不完全是我想要的......安装是一个数组,可能非常大......所以它必须以某种方式序列化或分成块以存储在数据库中。将其存储到一个文档中会使其太大..
  • 感谢您的帮助。但我需要以某种方式存储到多个文件中,这样它就不会创建一个大文档..
  • 好的,让我们用新的解决方案回复您。 @MuhammadAli
猜你喜欢
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 2016-03-12
相关资源
最近更新 更多