Question

I have a Thread document which has within it Comments, which are EmbeddedDocuments. I don't want to have all of the comments return at once, but instead display them ten at a time.

In PyMongo or with just MongoDB I know I could use the $slice operator, but I'm not sure of how to do this with mongoengine. If I do

Thread.objects.get(id=thread_id).comments[:10]

will it only fetch those ten from the database?

Cheers!

Was it helpful?

Solution

Support has been added in the dev branch and will make the next release!

You can return only 10 comments by using the fields and slice method:

thread = Thread.objects.fields(slice__comments=10).get(id=thread_id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top