Error importing BSON document, MongoDB

Sha*_*ang 0 javascript json mongodb bson

I am trying to import a sample BSON file test.bson into MongoDB. The server is already running, so I use

mongoimport --db test --collection foo --drop --file test.bson
Run Code Online (Sandbox Code Playgroud)

However, I get the following error:

2016-01-24T13:51:06.126-0500    connected to: localhost
2016-01-24T13:51:06.144-0500    Failed: error processing document #1: invalid character 'è' looking for beginning of value
2016-01-24T13:51:06.144-0500    imported 0 documents
Run Code Online (Sandbox Code Playgroud)

(1) How does one get around this error, invalid character 'è' looking for beginning of value? What exactly should I do to access the data in test.bson?

(2) Is there any other way to import data using MongoDB? If I am in the shell and type

$mongo
Run Code Online (Sandbox Code Playgroud)

how can I import json/bson files?

(BSON is a binary representation of JSON with additional type information.)

小智 5

我认为您需要mongorestore而不是mongoimport。所以尝试

mongorestore --db test --collection foo --drop test.bson
Run Code Online (Sandbox Code Playgroud)

从文档:

警告:

避免将mongoimport和mongoexport用于完整实例生产备份。它们不能可靠地保留所有丰富的BSON数据类型,因为JSON只能表示BSON支持的类型的子集。如MongoDB备份方法中所述,使用mongodump和mongorestore来实现这种功能。

mongoimport和mongoexport用于与数据类似json的表示形式(尽管它也适用于CSV和TSV)。mongorestore和mongoexport用于bson。