0

I need to delete all information but to persist objectId, because it can be referenced to outside my function (so just clear all fields, do not recreate)
I've heard i can do it with update() with no args or something like that.
Of course i can do it manually, but i'm sure there is a better solution

B. Semchuk
  • 1
  • 1
  • 9

1 Answers1

0

Use a bunch of batch writes to replace document with one that is empty e.g.

from pymongo import MongoClient, ReplaceOne

client = MongoClient()
db = client.test

requests = [ReplaceOne(doc, {}) for doc in db.collection.find()]
result = db.collection.bulk_write(requests)
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • I would prefer mongoengine if possible, btw i changed strategy a little if anyone is interested you can do something like this: ```foo = MyModel.objects.get(name='needed') bar = MyModel.create(id=foo.id)``` – B. Semchuk Jun 22 '18 at 12:31