mongodb - 如何将 bson 和 json 文件导入 MongoDB?

标签 mongodb

我有以下来自 https://github.com/Apress/def-guide-to-mongodb/tree/master/9781484211830/The%20Definitive%20Guide%20to%20MongoDBbsonjson 文件

$ ls .
aggregation.bson  aggregation.metadata.json  mapreduce.bson  mapreduce.metadata.json  storage.bson  text.json

如何将它们导入 MongoDB?

我尝试将它们中的每一个都导入为一个集合,但失败了:

$ mongorestore -d test -c aggregation 
2018-07-18T01:44:25.376-0400    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-07-18T01:44:25.377-0400    using default 'dump' directory
2018-07-18T01:44:25.377-0400    see mongorestore --help for usage information
2018-07-18T01:44:25.377-0400    Failed: mongorestore target 'dump' invalid: stat dump: no such file or directory

我不确定我是否正确指定了文件aggregation.bson,但上面的命令是我从书中的类似示例中学到的。

谢谢。


更新

下面,为什么第一个失败,第二个成功?我应该使用哪个命令?

$ mongoimport -d test -c aggregation --file aggregation.bson 
2018-07-18T09:45:44.698-0400    connected to: localhost
2018-07-18T09:45:44.720-0400    Failed: error processing document #1: invalid character 'º' looking for beginning of value
2018-07-18T09:45:44.720-0400    imported 0 documents

$ mongoimport -d test -c aggregation --file aggregation.metadata.json 
2018-07-18T09:46:05.058-0400    connected to: localhost
2018-07-18T09:46:05.313-0400    imported 1 document

最佳答案

mongoimport --db dbName --collection collectionName --type json --file fileName.json

更新:

C:\Program Files\MongoDB\Server\4.0\bin>mongorestore -d test -c aggregation aggregation.bson
2018-07-19T10:28:39.963+0300    checking for collection data in aggregation.bson
2018-07-19T10:28:40.099+0300    restoring test.aggregation from aggregation.bson
2018-07-19T10:28:41.113+0300    no indexes to restore
2018-07-19T10:28:41.113+0300    finished restoring test.aggregation (1000 documents)
2018-07-19T10:28:41.113+0300    done

所以我尝试了一下,它对我来说效果很好,你的 bin 文件夹中是否有该文件,或者你使用的命令是否不完整?

db.aggregation.find().pretty().limit(2)                                                                               
{
        "_id" : ObjectId("51de841747f3a410e3000001"),
        "num" : 1,
        "color" : "blue",                                                                                                       
        "transport" : "train",
        "fruits" : [
                "orange",
                "banana",
                "kiwi"
        ],
        "vegetables" : [
                "corn",
                "broccoli",
                "potato"
        ]
}
{
        "_id" : ObjectId("51de841747f3a410e3000005"),
        "num" : 5,
        "color" : "yellow",
        "transport" : "plane",
        "fruits" : [
                "lemon",
                "cherry",
                "dragonfruit"
        ],
        "vegetables" : [
                "mushroom",
                "capsicum",
                "zucchini"
        ]
}

关于mongodb - 如何将 bson 和 json 文件导入 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394444/

相关文章:

javascript - 如何使用 mongodb (node.js) 创建集合中所有值的数组

MongoDB 到 DynamoDB

java - 在 birt 中创建组并分析数组列表中的数据

MongoDB 如何添加条件 $match 条件

mongoDB c++11驱动程序获取插入文档的ID

MongoDB 的 Java 驱动程序 : Can't resolve function toJson()

java - Spring Data Mongodb 的性能问题

设置身份验证后,Mongodb 4 服务无法启动 - 错误 1053

java - mongoTemplate.findAll() 抛出异常

android - 我应该直接访问mongodb吗?