Any way to clear values in an index without dropping it?

Goal: DevOps pipeline that will drop Collections in target system, then refresh them with an import from a source system.

I tried doing mongodump and mongorestore, but those break Device Sync replication, so not an option. Next idea was to just drop the data inside the Collections to preserve the schema, then restore from Source database. This would ensure the import would be a non-breaking change that would not break replication.

--eval "db.getCollectionNames().forEach(collection_name=>db[collection_name].deleteMany({}))"

However, the problem with this is the index still preserves the old values from the original data.

error: E11000 duplicate key error collection: __realm_sync_63750a5292e8ebc57ad100ee.client_meta_version_counter index: _id_ dup key:

Dropping the index would be a Breaking change, and MongoDB doesn’t let you drop an index with ID in it anyway.

So I’m not sure what the best path forward here is. If there is a way to “clear” the index without dropping it, that would be great. If that’s not possible, any other suggestions would be appreciated. These are small config tables, so we thought it was a cleaner path forward to move the entire table between environments, instead of trying to piece them together with updates, inserts, etc.

Thanks for any help.

Answered my own question again lol. I guess the key point I didn’t mention here was I was using mongrestore to import the new data. That process will also build the indexes as well, so that’s why I got the issue.

I used mongexport/mongoimport and now good to go !

2 Likes