Filtering an embedded list in MongoEngine

怎甘沉沦 提交于 2019-12-04 11:30:15

MongoDB doesn't support exactly this operation that you're requesting, and therefore neither does Mongoengine.

You can perform slicing operations on arrays (lists), but not ad-hoc filtering. Slicing in MongoDB arrays works similarly to slicing lists in Python, and you can do it with Mongoengine using the slice__ keyword syntax:

Main.objects.filter(subs__name__exists=True).fields(slice__subs=[0,2])

This will return the subs starting at index 0 (i.e. the first element) and returning two elements after that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!