Reorder MongoDB Document Fields
From MongoDB Compass --> Open MongoDB shell:
db.MY_COLLECTION_NAME.updateMany({}, [{$replaceWith: {$mergeObjects: [{"_id": "$_id","field1": "$field1","field2": "$field2"},"$$ROOT"]}}])
Specify the fields in the block in the order you want them.
Note this works with arrays and all data types.

Comments
Post a Comment