将json文件插入到mongodb中
我是mongodb中的新成员。 在Windows中安装mongodb后,我试图插入一个简单的json文件,使用下面的命令:
C:\>mongodb\bin\mongoimport --db test --collection docs < example2.json
我收到以下错误:
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.file
{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bwayne@Wayneenterprises.com"} {"FirstName": "Lucius", "LastName": "Fox", "Email": "lfox@Wayneenterprises.com"} {"FirstName": "Dick", "LastName": "Grayson", "Email": "dgrayson@Wayneenterprises.com"}
我需要做什么来将新的json文件导入到mongodb中?
使用
mongoimport --jsonArray --db test --collection docs --file example2.json
它可能因为换行符而搞乱了。
下面的命令为我工作
mongoimport --db test --collection docs --file example2.json
当我在每个文档中的Email
属性之前删除额外的换行符。
example2.json
{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bwayne@Wayneenterprises.com"} {"FirstName": "Lucius", "LastName": "Fox", "Email": "lfox@Wayneenterprises.com"} {"FirstName": "Dick", "LastName": "Grayson", "Email": "dgrayson@Wayneenterprises.com"}
导入JSON文件时使用下面的命令
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
这对我有用 – (从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
在MS Windows中,mongoimport命令必须在正常的Windows命令提示符下运行,而不是在mongodb命令提示符下运行。
这发生在几周前。 mongoimport的版本太旧了。 一旦我更新到最新版本,它成功运行并导入所有文件。
参考: http : //docs.mongodb.org/master/tutorial/install-mongodb-on-ubuntu/?_ga=1.11365492.1588529687.1434379875
mongoimport --jsonArray -d DatabaseN -c collectionName /filePath/filename.json