Mongoengine old data archive

I have a huge MongoDB database running on mongoengine where the objects have a date. To simplify the work, I want to archive old objects, but keep them somewhere.

I read the documentation and came across switch_db and switch_collection . However, I can neither work nor work.

In both cases, the documentation refers to two use cases.

  • Like a QuerySet operation:

     user = User.objects.get(id=user_id) user.switch_collection('old-users') user.save() 

    The problem with this is that it only works for a single object. Archiving multiple documents is not possible.

  • Like context_manager :

     with switch_collection(Group, 'group1') as Group: Group(name="hello testdb!").save() # Saves in group1 collection 

    Using this, I canโ€™t even execute the request, getting the following error:

     ValidationError (Document:None) (Field is required... 

I tried to find a way to archive data using mongoengine, but none of the options worked. Do you have any suggestions?

+7
python mongoengine
source share
1 answer

If you have access to the MongoDB instance itself, why not just run the query directly against this, instead of jumping through the MongoEngine hoops?

you can rename your current users collection to users-old with this command:

http://docs.mongodb.org/manual/reference/command/renameCollection/

and then run a simple script to copy the new values โ€‹โ€‹to the new users collection

0
source share

All Articles