Skip to content

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

Description

@shahafabileah

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions