问题描述
MongoDB 的聚合 $min 与 查询修饰符 $min 与 find().sort() 返回the_field 的最小值:db.the_collection.find().sort({the_field:1}).limit(1)
?
What is the difference among MongoDB's aggregation $min versus query modifier $min versus find().sort() that returns the minimum of the_field with: db.the_collection.find().sort({the_field:1}).limit(1)
?
如果每分钟大约有几百次调用来检索集合中的最小元素并每次都独立处理它,我应该使用哪个?
附带问题:有人可以使用 $min 向我展示正确的语法,以便为我提供集合中字段的最小值吗?db.the_collection.find().min({the_field:10})
不起作用.
Side question: Can someone show me the correct syntax using either $min to give me the minimum of a field in a collection? db.the_collection.find().min({the_field:10})
doesn't work.
推荐答案
要获取 'the_field' 最低值的文档,您应该使用db.the_collection.find().sort({the_field:1}).limit(1)
所以在这里我们首先对文档进行排序,然后从查询中看到第一个从中取出.
To get the document for lowest value of 'the_field' you should use db.the_collection.find().sort({the_field:1}).limit(1)
So over here we are Sorting the document first and then taking the first one out of it as you can see from the query.
聚合 $min :
当我们将文档分组为单个文档并且我们希望将这个单个文档的值保持为从它被分组的所有文档中的最小值时使用它.
Aggregation $min :
It is used when we are grouping the document into a single document and we want to keep the value of the this single document as minimum of all the documents from where it was grouped.
$min 运算符:
它用于特定索引的包含下限以约束 find() 的结果.简单来说,如果 the_field 被索引并且我们想对 find() 保持一些约束,那么我们可以使用它.一般用于提高性能.
您输入的语法是正确的,但它需要一个索引字段,结果将与您实际想要的不同.
$min operator :
It is used for inclusive lower bound for a specific index in order to constrain the results of find(). In simple words, if the_field is indexed and we want to keep some constraint on the find() then we can use it. It is generally used for improving the performance.
The syntax which you were entering was correct but it requires an Indexed field and the result will be different from what you actually want.
这篇关于MongoDB 的 $min 是多少?这与 find().sort({the_field: 1}).limit(1) 有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!