如何将bson和json文件导入MongoDB?
如何将bson和json文件导入MongoDB?
我有以下来自于https://github.com/Apress/def-guide-to-mongodb/tree/master/9781484211830/The%20Definitive%20Guide%20to%20MongoDB的bson和json文件:\n
$ ls . aggregation.bson aggregation.metadata.json mapreduce.bson mapreduce.metadata.json storage.bson text.json
\n我该如何将它们导入到MongoDB中?\n我尝试将它们中的每一个作为一个集合导入,但是失败了:\n
$ mongorestore -d test -c aggregation 2018-07-18T01:44:25.376-0400 使用--db和--collection参数仅在从BSON文件恢复时使用。其他用法已过时且将来将不存在;请使用--nsInclude代替 2018-07-18T01:44:25.377-0400 使用默认的'dump'目录 2018-07-18T01:44:25.377-0400 有关用法信息,请参阅mongorestore --help 2018-07-18T01:44:25.377-0400 失败:mongorestore目标'dump'无效:stat dump:没有这个文件或目录
\n我不确定是否正确指定了文件aggregation.bson
,但上述命令是我从一本类似的书籍中学到的。\n谢谢。\n
\n更新\n在下面的命令中,为什么第一个失败了而第二个成功了?我应该使用哪个命令?\n$ mongoimport -d test -c aggregation --file aggregation.bson \n2018-07-18T09:45:44.698-0400 连接到:localhost\n2018-07-18T09:45:44.720-0400 失败:处理文档#1时出错:查找值的开始时无效字符\'º\'\n2018-07-18T09:45:44.720-0400 导入了0个文档\n$ mongoimport -d test -c aggregation --file aggregation.metadata.json \n2018-07-18T09:46:05.058-0400 连接到:localhost\n2018-07-18T09:46:05.313-0400 导入了1个文档\n
你可以使用以下命令将bson和json文件导入到MongoDB中:
mongoimport --db dbName --collection collectionName --type json --file fileName.json
如果你想要一次性上传所有集合,你可以使用mongorestore
命令。以下是一个示例:
C:\Program Files\MongoDB\Server\4.0\bin>mongorestore -d test -c aggregation aggregation.bson
这将从aggregation.bson
文件中恢复test
数据库中的aggregation
集合。你还可以使用db.collection.find().pretty().limit(2)
命令来查看导入的数据。