Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

item_frequencies returns {None: 1} if there's a single document with an empty list #761

Closed
shahafabileah opened this issue Sep 12, 2014 · 2 comments · May be fixed by #2517
Closed

item_frequencies returns {None: 1} if there's a single document with an empty list #761

shahafabileah opened this issue Sep 12, 2014 · 2 comments · May be fixed by #2517

Comments

@shahafabileah
Copy link

I noticed a problem where item_frequencies unexpectedly returns {None: 1}.

Here's my repro:

import mongoengine as me

import settings

me.connect('test', host=settings.MONGO_URL)

# Define a document type with a list field

class TestDocument(me.Document):
    id = me.SequenceField(primary_key=True)
    fruit = me.ListField(me.StringField())

# Insert one document that has a non-empty list

doc1 = TestDocument()
doc1.fruit = ['apple', 'orange']
doc1.save()

# Get the item_frequencies

item_frequencies = TestDocument.objects().item_frequencies('fruit')

print item_frequencies

# Change the document.  Make it an empty list.

doc1.fruit = []
doc1.save()

# Get the item_frequencies again

item_frequencies = TestDocument.objects().item_frequencies('fruit')

print item_frequencies

Here's the output:

(venv)~/work> python -m data.item_frequencies_test
{u'orange': 1, u'apple': 1}
{None: 1}

Note that if the list starts out as empty then the problem doesn't repro. It only repros if the document first has a non-empty list and then has that list cleared out.

@DavidBord
Copy link
Contributor

The actual problem here is that setting doc1.fruit = [] actually unsets the attribute

@bagerard
Copy link
Collaborator

bagerard commented Oct 7, 2024

Closing as it's a duplicate of #2517

@bagerard bagerard closed this as completed Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants