polandeer
polandeer

Reputation: 396

Getting one Field prior to an atomic update in MongoEngine

Assuming a schema like below:

class DocA(Document):
    owner = ReferenceField(User)
    strings = ListField(StringField)

If I want to check if the logged in user is the owner and then add a new string to stringlist, I currently have to access the database twice:

if self.has_perm(DocA.objects(id=someid).only('owner').first()):
    DocA.objects(id=someid).update_one(push__strings="New String")

Is there a better way to do this?

Upvotes: 0

Views: 92

Answers (1)

Ross
Ross

Reputation: 18111

Given has perm is just checking the owner you can do this in a single query:

DocA.objects(id=someid, user=logged_in_user).update_one(push__strings="New String")

Upvotes: 1

Related Questions