Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何将sails.js disk.db导入mongodb_Json_Node.js_Mongodb_Sails.js - Fatal编程技术网

Json 如何将sails.js disk.db导入mongodb

Json 如何将sails.js disk.db导入mongodb,json,node.js,mongodb,sails.js,Json,Node.js,Mongodb,Sails.js,所以我有一个使用web框架的nodejs应用程序。在我开发时,我将适配器设置为sails磁盘。来自disk.db的样本数据如下 { "data": { "authentication": [ { "user_id": 1, "username": "user1", "encrypted_password": "password here",

所以我有一个使用web框架的nodejs应用程序。在我开发时,我将适配器设置为sails磁盘。来自disk.db的样本数据如下

{
    "data": {
        "authentication": [
            {
                "user_id": 1,
                "username": "user1",
                "encrypted_password": "password here",
                "createdAt": "2014-05-12T07:40:24.901Z",
                "updatedAt": "2014-08-18T19:37:22.851Z",
                "id": 1,
                "email": "user1@email.com"
            }
        ],
        "user": [
            {
                "name": "user0001",
                "email": "user1@email.com",
                "role": [
                    "admin"
                ],
                "phone": [
                    {}
                ],
                "status": "Active",
                "createdAt": "2014-05-12T07:48:11.028Z",
                "updatedAt": "2014-05-24T08:12:41.646Z",
                "id": 1,
                "username": "user1"
            }
        ]
    }
}
这在我本地的机器上运行得很好。 现在,我准备将其部署到生产环境中,并使用mongodb作为我的数据库。我已经设置了所有连接,并且可以成功地在我的数据库上连接


我的问题是:在维护json数据格式或集合的同时,是否有一种方法可以将本地磁盘数据库(
disk.db
)导入mongodb?或者我需要在mongodb上手动创建我的集合并导入每个集合?

没有工具或官方方法使用SAIL进行此类迁移。根据您拥有的模型数量以及为您编写迁移脚本的繁琐程度,一个选项是镜像您的模型——即,如果您有一个
User.js
,则创建一个新模型
UserNew.js
,如下所示:

module.exports = {

    connection: 'mongoDbConnection',
    tableName: 'user',
    attributes: <same as User.js attributes>

}
把他们调过来


这将是一次性的,之后您可以删除原始的
User.js
,并将
UserNew.js
重命名为
User.js
。相当骇客,但同样取决于我们谈论的数据量,这可能是最快的选择

我以sgress454的答案为基础,将其翻过来。我创建了一个OldUserLocation.js模型,而不是一个新用户

module.exports = {
    connection: 'localDiskDb',
    tableName: 'user',
    attributes: {
    //same as in User.js
}
}

在默认模型中,我指定了mongodb

在引导功能中,我添加了以下内容:

User.find().exec(function(err, users){
    if(!err && users.length == 0){
        OldUserLocation.find().exec(function(err, oldUsers) {
            if(!err && oldUsers.length >0){
                User.create(oldUsers).exec(function (err){
                    cb();
                });
            }
            else{
                cb();
            }
        });
    }
    else{
        cb();  
    } 
});

简单地说,如果mongo DB中的用户模型没有用户,那么我将尝试从localdisk DB导入。如果已经有用户,我假设导入已经完成。

我看到的另一种黑客方式是:

  • 打开
    .tmp/localDiskDb.db
  • 复制对象
  • 使用运行POST查询 邮递员
  • 将查询设置为POST
  • 在“主体”选项卡中设置端点URL,选择“原始”、“JSON”将对象粘贴到框中
  • 按发送

rince,重复一遍

您关心的是迁移实际数据记录,还是仅仅迁移集合?MongoDB将根据需要为您创建集合,因此如果您只想这样做,您无需执行任何操作。是的,实际的数据记录。
User.find().exec(function(err, users){
    if(!err && users.length == 0){
        OldUserLocation.find().exec(function(err, oldUsers) {
            if(!err && oldUsers.length >0){
                User.create(oldUsers).exec(function (err){
                    cb();
                });
            }
            else{
                cb();
            }
        });
    }
    else{
        cb();  
    } 
});