可以将Mongo数据保存到多个文档中

时间:2019-09-06 06:33:39

标签: node.js mongodb mongoose

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

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

return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: ('col1', 'col2')

1 个答案:

答案 0 :(得分:0)

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

}

我尝试使用20万条记录,但没有给出错误。