Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine Document.update() example

Tags:

mongoengine

Assuming that Venue is:

from mongoengine import *
from mongoengine_extras.fields import  AutoSlugField
class Venue(Document):
    name = StringField(required=True)
    venue_slug = AutoSlugField()

I want to update all my venue_slug fields based on the name. I try:

for v in Venue.objects():
    v(venue_slug = str(v.name)).update()

But I get:

v(venue_slug = str(v.name)).update()
TypeError: Error when calling the metaclass bases
'Venue' object is not callable

Is my update function correct? If you are not familiar with AutoSlugField() could you write an example for a StringField() update?

like image 223
Diolor Avatar asked Nov 12 '13 21:11

Diolor


People also ask

How do I update MongoEngine?

MongoEngine provides the following methods for atomic updates on a queryset. update_one() − Overwrites or adds first document matched by query. update() − Performs atomic update on fields matched by query. modify() − Update a document and return it.

How do you update a document?

On the Document Details page, click EDIT / UPDATE.

What is an example of an update operation modifier in MongoDB?

The following modifiers are available to update operations. For example - in db. collection. update() and db.


1 Answers

Your code incorrect. Try:

for v in Venue.objects():
    v.update(set__venue_slug=str(v.name))

See documentation: http://docs.mongoengine.org/guide/querying.html#atomic-updates.

like image 66
tbicr Avatar answered Nov 24 '22 01:11

tbicr