Dump Mongo Collection into JSON format

后端 未结 5 875
小蘑菇
小蘑菇 2020-12-04 07:10

Is there any way to dump mongo collection into json format? Either on the shell or using java driver.I am looking for the one with best performance.

5条回答
  •  庸人自扰
    2020-12-04 07:52

    Use mongoexport/mongoimport to dump/restore a collection:

    Export JSON File:

    mongoexport --db --collection --out output.json

    Import JSON File:

    mongoimport --db --collection --file input.json

    WARNING mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.

    Also, http://bsonspec.org/

    BSON is designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.

    In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinData and Date data types.

提交回复
热议问题