[英]Using pymongo, how do you find all documents, such that an embedded list in each document contains greater than X elements?
我想在MongoDB上运行查找查询,搜索集合中的一组文档,其中每个文档包含元素列表,并且仅返回列表中的元素数大于X的文档。
这将返回len(消息)== X的所有文档:
db.log.find({'messages':{'$size':X}})
但如何获取len(消息)> = X的所有文件?
在MongoDB 2.2+中,您可以利用以下事实:您可以在查询对象键中使用数字数组索引来执行此操作:
# X is the minimum number of messages
X = 3
db.log.find({'messages.' + str(X-1): {'$exists': 1}})
您可以通过此管道在聚合框架中执行此操作:
db.collection.aggregate({$unwind:"$messages"},
{$group:{_id:"$_id", msgCount:{$sum:1}}},
{$match:{msgCount:{$gte:<SIZE>}}}
);
填写您要查找的尺寸超过<SIZE>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.