问题
The documentation for mongoexport has this scary warning,
Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.
The page then goes on to say,
To preserve type information, mongoexport and mongoimport uses the strict mode representation for certain types.
What exactly are the types that mongoexport still doesn't represent properly, in spite of the "strict mode representation"?
Asking because mongorestore
has an extremely annoying limitation: it doesn't support an upsert option, which makes it impossible to use for synchronizing collections where only a few documents get updated. You'd have to --drop
the whole collection before restoring it entirely from scratch, which can be very time consuming for large collections, especially if text indexes need to be recreated.
回答1:
Turns out that the warning against mongoimport/export is obsolete. It's still a good idea to use mongodump/restore for speed, but mongoexport preserves type information using the MongoDB Extended JSON format. For example, a Date field is no longer silently converted to text, but instead to:
{ "$date": "<date>" }
where <date>
is the ISO-8601 YYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>
representation of the date. Regular expressions are converted to
{ "$regex": "<sRegex>", "$options": "<sOptions>" }
etc. These textual representation of the document fields are parsed by mongoimport, restoring the original types. See BSON Data Types and Associated Representations for more information.
The warning will hopefully be removed soon.
来源:https://stackoverflow.com/questions/56554640/which-mongodb-types-are-not-preserved-by-mongoimport-mongoexport