Create three practice documents
In an unused practice database, run this in mongosh. Open keplergrid_fields.people in KeplerGrid and paste the filter below into Documents before running an update.
const people = db.getSiblingDB("keplergrid_fields").people;
people.insertMany([
{ _id: "P-1", nickname: "Jay", legacyTag: "old" },
{ _id: "P-2", nickname: "Lee", displayName: "Lee S.", legacyTag: "keep" },
{ _id: "P-3", displayName: "Sam", legacyTag: "keep" }
]);
Preview filter { nickname: { $exists: true }, displayName: { $exists: false } } should match only P-1.
Rename a field while preserving destination values
When the destination field exists, MongoDB’s $rename can replace it. Keep the destination check in your write filter to preserve existing values.
const people = db.getSiblingDB("keplergrid_fields").people;
people.updateMany(
{ nickname: { $exists: true }, displayName: { $exists: false } },
{ $rename: { nickname: "displayName" } }
);
Rerun the query and inspect P-1. It now has displayName: "Jay". P-2 still has displayName: "Lee S." and retains its original nickname.
Remove only the intended field
Preview the matches for the next filter before running it in mongosh. $unset removes the named field from the matching documents.
const people = db.getSiblingDB("keplergrid_fields").people;
people.updateMany(
{ _id: "P-1", legacyTag: { $exists: true } },
{ $unset: { legacyTag: "" } }
);
Confirm that only P-1 lost legacyTag. For a real bulk update, validate the scope on a staging copy and plan its recovery before writing.
Edit a single document in KeplerGrid · MongoDB update operator reference