Question

My application is a simple TODO list, this are my classes with MongoEngine...

class Task(EmbeddedDocument):
    content = StringField()
    slug = StringField()
    position = IntField()
    priority = IntField()
    status = IntField()

class List(EmbeddedDocument):
    name = StringField()
    slug = StringField()
    tasks = ListField(EmbeddedDocumentField(Task))

class User(Document):
    email = StringField()
    twitter = StringField()
    lists = ListField(EmbeddedDocumentField(List))

I push to the User lists with...

list = List()
list.name = request.form['name']
list.slug = slugify(request.form['name'])
User.objects(twitter=session['username']).update_one(push__lists=list)

The logic is that the user have multiple lists and each lists can have multiple tasks.

Now I want to push a task in certain list, but I can't find the way with MongoEngine. The approach could be some like this:

User.objects(twitter=session['username']).update_one(push__lists__listname__task=list)

Which is the correct way of doing this?

Was it helpful?

Solution

This isn't currently possible, either with MongoDB or MongoEngine.

The development branch of MongoEngine has some support for the $ positional operator, but MongoDB only supports using the positional operator once per update -- that is you cannot use it to update lists nested within lists (even if indirectly, as in your example).

If you need to perform atomic updates on the todo lists, you should reconsider your data model to allow this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top