查看: 66|回覆: 0

MongoDB导入数据库异常——Failed: cannot decode array into a D

[複製鏈接]

2

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2010-5-23
發表於 2020-6-12 20:49:00 | 顯示全部樓層 |閲讀模式

对于单个json文件,如何导入mongodb数据库?

答:使用mongoimport命令

mongoimport --db [databaseName] --collection [collectionName] --file [filePath]

用命令行进入安装MongoDB的bin目录下,我安装路径是C:\Program Files\MongoDB\Server\4.2\bin,输入上述命令。输入上面三个参数

[databaseName] => 数据库名
[collectionName] => 集合名
[filePath] => 文件路径



示例【指令也可以简写成只写首字母的形式】:

    mongoimport --db playground --collection user --file ./test.json

playground为我创建的数据库,user为集合名,./test.json为我json文件所在路径



导入的时候可能会报错,说“mongodb Failed: cannot decode array into a D”



原因:这是因为导入了一个json的数组,只需把参数–file改成–jsonArray即可



打开看一下我们要导入的数据库集合的具体内容:

[{

    "name": "Tonny",
    "author": "Jim",
    "isPublished": true
}, {

    "name": "Javascript",
    "author": "Alexander",
    "isPublished": true
}, {
    "name": "Javascript",
    "author": "Alexander",
    "isPublished": true
}, {
    "name": "C#",
    "author": "Alex",
    "isPublished": true
}, {
    "name": "C#",
    "author": "Ezharjan",
    "isPublished": true
}]




发现确实不是file而是数组,解决方案:


    mongoimport --db test --collection user --jsonArray ./test.json



结果——导入成功:




作者:艾孜尔江

回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部