0

How do I add an attribute to all documents in collection using mongoengine? I've tried several ways:

for d in MyDocuments.objects:
    d.attr = 'test'
    d.save()

This doesn't give any errors but it doesn't add new attribute to document.

I've also tried

MyDocuments.objects.all().update(set__attr='test')

This gives InvalidQueryError: Cannot resolve field "attr"

The same error happens when I try

for d in MyDocuments.objects:
    d.update(set__attr='test')

Specifically I'm trying to add _cls attribute because I enabled inheritance and I already have some objects in database that don't have that attribute.

NST
  • 724
  • 9
  • 20

1 Answers1

0

Finally this worked for me:

MyDocuments._get_collection().update_many({}, {'$set': {'_cls': 'MyDocuments.TTR'}})
NST
  • 724
  • 9
  • 20