mongodb 将json文件插入mongodb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19441228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert json file into mongodb
提问by Jay
I am new to MongoDB. After installing MongoDB in Windows I am trying to insert a simple json file using the following command:
我是 MongoDB 的新手。在 Windows 中安装 MongoDB 后,我尝试使用以下命令插入一个简单的 json 文件:
C:\>mongodb\bin\mongoimport --db test --collection docs < example2.json
I am getting the following error:
我收到以下错误:
connected to: 127.0.0.1
Fri Oct 18 09:05:43.749 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:43
Fri Oct 18 09:05:43.750
Fri Oct 18 09:05:43.750 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:42
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:44
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 check 0 0
Fri Oct 18 09:05:43.752 imported 0 objects
Fri Oct 18 09:05:43.752 ERROR: encountered 6 error(s)s
example2.json
示例2.json
{"FirstName": "Bruce", "LastName": "Wayne",
"Email": "[email protected]"}
{"FirstName": "Lucius", "LastName": "Fox",
"Email": "[email protected]"}
{"FirstName": "Dick", "LastName": "Grayson",
"Email": "[email protected]"}
What do I need to do to import new json file into mongodb?
我需要做什么才能将新的 json 文件导入 mongodb?
回答by Leon
Use
用
mongoimport --jsonArray --db test --collection docs --file example2.json
Its probably messing up because of the newline characters.
由于换行符,它可能会搞砸。
回答by mithunsatheesh
Below command worked for me
以下命令对我有用
mongoimport --db test --collection docs --file example2.json
when i removed the extra newline character before Email
attribute in each of the documents.
当我删除Email
每个文档中属性之前的额外换行符时。
example2.json
示例2.json
{"FirstName": "Bruce", "LastName": "Wayne", "Email": "[email protected]"}
{"FirstName": "Lucius", "LastName": "Fox", "Email": "[email protected]"}
{"FirstName": "Dick", "LastName": "Grayson", "Email": "[email protected]"}
回答by nanusdad
This worked for me - ( from mongo shell )
这对我有用 - (来自 mongo shell)
var file = cat('./new.json'); # file name
use testdb # db name
var o = JSON.parse(file); # convert string to JSON
db.forms.insert(o) # collection name
回答by Sumit Kamboj
Use below command while importing JSON file
导入 JSON 文件时使用以下命令
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
回答by user10383785
the following two ways work well:
以下两种方式效果很好:
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs < example2.json
if the collections are under a specific user, you can use -u -p --authenticationDatabase
如果集合属于特定用户,则可以使用 -u -p --authenticationDatabase
回答by Vijay Prajapati
mongoimport --jsonArray -d DatabaseN -c collectionName /filePath/filename.json
回答by Steffi Keran Rani J
This solution is applicable for Windows machine.
此解决方案适用于 Windows 机器。
MongoDB needs data directory to store data. Default path is
C:\data\db
. In case you don't have the data directory, create one in your C: drive. (P.S.: data\db means there is a directory named 'db' inside the directory 'data')Place the json you want to import in this path:
C:\data\db\
.Open the command prompt and type the following command
mongoimport --db databaseName --collections collectionName --file fileName.json --type json --batchSize 1
MongoDB 需要数据目录来存储数据。默认路径是
C:\data\db
. 如果您没有数据目录,请在 C: 驱动器中创建一个。(PS:data\db 表示在目录'data'中有一个名为'db'的目录)将要导入的 json 放在此路径中:
C:\data\db\
.打开命令提示符并键入以下命令
mongoimport --db databaseName --collections collectionName --file fileName.json --type json --batchSize 1
Here,
这里,
- databaseName : your database name
- collectionName: your collection name
- fileName: name of your json file which is in the path C:\data\db
- batchSize can be any integer as per your wish
- databaseName :您的数据库名称
- 集合名称:您的集合名称
- 文件名:您的 json 文件的名称,该文件位于路径 C:\data\db
- batchSize 可以是您希望的任何整数
回答by UdayKiran Pulipati
In MongoDB To insert Json array data from file(from particular location from a system / pc) using mongo shell command. While executing below command, command should be in single line.
在 MongoDB 中使用 mongo shell 命令从文件(来自系统/PC 的特定位置)插入 Json 数组数据。在执行下面的命令时,命令应该在单行中。
var file = cat('I:/data/db/card_type_authorization.json'); var o = JSON.parse(file); db.CARD_TYPE_AUTHORIZATION.insert(o);
var file = cat('I:/data/db/card_type_authorization.json'); var o = JSON.parse(file); db.CARD_TYPE_AUTHORIZATION.insert(o);
JSON File:card_type_authorization.json
JSON 文件:card_type_authorization.json
[{
"code": "visa",
"position": 1,
"description": "Visa",
"isVertualCard": false,
"comments": ""
},{
"code": "mastercard",
"position": 2,
"description": "Mastercard",
"isVertualCard": false,
"comments": ""
}]
回答by Prakash Singh
Open command prompt separately and check:
单独打开命令提示符并检查:
C:\mongodb\bin\mongoimport --db db_name --collection collection_name< filename.json
C:\mongodb\bin\mongoimport --db db_name --collection collection_name< filename.json
回答by Baraka215
In MS Windows, the mongoimport command has to be run in a normal Windows command prompt, not from the mongodb command prompt.
在 MS Windows 中,mongoimport 命令必须在普通的 Windows 命令提示符下运行,而不是在 mongodb 命令提示符下运行。