MongoEngine / MongoEngine/mongoengine

document.update(**kwargs) writes to MongoDB but not updating fields of document

Open
#650 15 comments 5 reactions 1 assignee View on GitHub

@thedrow is already working on this.

Since Jul 1, 2014.

Discussion Enhancement
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Seems like batch updating with document.update(**kwargs) only affects DB, not current document. Here is an example:

from mongoengine import *

connect("test")


class SomeDocument(Document):
    title = StringField()

    def __unicode__(self):
        return self.title


SomeDocument.drop_collection()

doc = SomeDocument(title="Initial title")
doc.save()

print "Updating fields one by one and finally save()"
doc = SomeDocument.objects[0]
doc.title = "New title 1"
doc.save()
doc_reloaded = SomeDocument.objects[0]
print "Saved: %s, Loaded: %s" % (doc, doc_reloaded)

print ""

print "Batch updating with document.update(**kwargs)"
doc = SomeDocument.objects[0]
doc.update(**{ "set__title": "New title 2" })
doc_reloaded = SomeDocument.objects[0]
print "Saved: %s, Loaded: %s" % (doc, doc_reloaded)

Output

Updating fields one by one and finally save()
Saved: New title 1, Loaded: New title 1

Updating with update()
Saved: New title 1, Loaded: New title 2

Expected behaviour is to update field's values after performing update().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.