Mongo DB - 以 JSON 扩展模式导出 [英] Mongo DB - export in JSON extended mode

查看:53
本文介绍了Mongo DB - 以 JSON 扩展模式导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何命令或工具可以以 JSON 扩展模式而不是默认模式严格模式导出 mongo 集合.

I'd like to know if there is any command or tool to export mongo collections in JSON extended mode instead of strict mode which is the default mode.

欢迎提出任何建议.

谢谢!

推荐答案

mongoexport> 和 mongoimport is 扩展 JSON:

BSON 类型的严格模式表示符合 JSON RFC.任何 JSON 解析器都可以将这些严格模式表示解析为键/值对;然而,只有 MongoDB 的内部 JSON 解析器也能识别格式所传达的类型信息.

Strict mode representations of BSON types conform to the JSON RFC. Any JSON parser can parse these strict mode representations as key/value pairs; however, only the MongoDB’s internal JSON parser also recognizes the type information conveyed by the format.

mongo shell 也有更松散的表示,它类似于 JSON,但允许使用无效 JSON 的语法.例如,shell 语法允许不带引号的字符串值代表扩展数据类型的辅助函数(例如 ISODate()).

There is also a looser representation for the mongo shell which is JSON-like, but allows syntax which is not valid JSON. For example, the shell syntax allows unquoted string values which represent helper functions (such as ISODate()) for extended data types.

扩展 JSON:

{
   "_id":{
       "$oid":"55d39fac2ba0663a655bee54"
   },
   "day":{
        "$date":"2015-08-18T21:12:12.945Z"
   }
}

mongo shell 语法(类 JSON):

mongo shell syntax (JSON-like):

{
  "_id": ObjectId("55d39fac2ba0663a655bee54"),
  "day": ISODate("2015-08-18T21:12:12.945Z")
}

mongo shell 导出

如果你真的想以 mongo shell 语法导出一个集合,你可以将输出通过管道传输到一个文件中.

Exporting from the mongo shell

If you really want to export a collection in mongo shell syntax, you can pipe the output to a file.

这种格式只能被 mongo shell 理解,如果你试图打印一个大集合,你可能会遇到限制:

This format will only be understood by the mongo shell, and you will likely run into limitations if you are trying to print a large collection:

mongo server:port/dbname --eval "printjson(db.collection.find().toArray())"

用于将集合导入/导出为 JSON 的标准命令行工具是 mongoimportmongoexport.这些通常不适合数据库备份——有关支持的备份和恢复教程,请参阅 MongoDB 手册.

The standard command line tools for importing/exporting collections as JSON are mongoimport and mongoexport. These generally aren't suitable for database backups -- see the MongoDB manual for supported Backup and Recovery tutorials.

这篇关于Mongo DB - 以 JSON 扩展模式导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆