开发者

Slicing in mongoengine

开发者 https://www.devze.com 2023-03-01 12:46 出处:网络
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.

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 datab开发者_JS百科ase?

Cheers!


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消