MongoEngine / MongoEngine/mongoengine

How to modify the last item of a list field by positional operator?

Open
#2,758 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I need to update the last item of a list field positional operator something like below

class Tag(EmbeddedDocument):
    name = StringField(required=True)
    description = StringField()


class Blog(Document):
    tags = EmbeddedDocumentListField(Tag, required=True)
    content = StringField(required=True)

blog = Blog.objects.filter(id=id).first()
blog.modify(set__tags__0="Python")

In the code above I modify the first item of the tags field. It is not possible to use -1 in the keyword. So how to modify the last item of a list field by positional argument?

I know it is possible something like below

blog.tags[-1]  = "Python"
blog.save()

However, I implemented a custom signal and put it into the modify method for decoupling audit logs. If I use the save method for updates those updates will be missed in the audit logs.

What I need is something like this

blog.modify(**{"set__tags__-1": "Python"})

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.

Research direction

Start by tracing how the supplied blog.modify call converts set__tags__0 into an update and how MongoDB handles positional list paths. Compare that path with the existing blog.tags[-1] and blog.save() behavior; done means the last tag can be changed through modify while the existing audit signal still records the update.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.