Having this similar model and MongoEngine docs:
class Post(Document): title = StringField() tags = ListField(StringField()) post1 = Post(title='Fun with MongoEngine', tags=['mongodb', 'mongoengine']).save() post2 = Post(title='Loving Mongo', tags=['mongodb']).save()
You will store this:
{ "tags": [ "mongodb", "mongoengine" ], "title": "Fun with MongoEngine" } { "tags": [ "mongodb" ], "title": "Loving Mongo" }
In MongoDB, when run, find() returns documents matching your query.
Thus, using the following query, you will get an array of documents (Post objects) that have the mongodb tag in the tags the array field. (in fact, the tag array is concatenated with an empty string and matched if it contains the value mongodb):
Post.objects(tags__contains='mongodb')
(a filter is just an alias for an object constructor)
Therefore, after you get the frequencies of the items, you need to get the ones that interest you.
tag_cloud = Post.objects(tags__contains='mongodb').item_frequencies('tags') > print tag_cloud {'mongodb': 2, 'mongoengine': 1} > tag_cloud['mongodb'] 2
source share