如何在mongodb中查找最小值 [英] How to find min value in mongodb
本文介绍了如何在mongodb中查找最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何做等同于
SELECT
MIN(Id) AS MinId
FROM
Table
使用MongoDB吗?
with MongoDB?
看起来我将不得不使用MapReduce,但是我找不到任何显示该操作方法的示例.
It looks like I will have to use MapReduce but I can't find any example that shows how to do this.
推荐答案
您可以使用sort
和limit
的组合来模拟min
:
You can use a combination of sort
and limit
to emulate min
:
> db.foo.insert({a: 1})
> db.foo.insert({a: 2})
> db.foo.insert({a: 3})
> db.foo.find().sort({a: 1}).limit(1)
{ "_id" : ObjectId("4df8d4a5957c623adae2ab7e"), "a" : 1 }
sort({a: 1})
是对a
字段的升序(最小优先)排序,然后我们仅返回第一个文档,该文档将是该字段的最小值.
sort({a: 1})
is an ascending (minimum-first) sort on the a
field, and we then only return the first document, which will be the minimum value for that field.
编辑:请注意,这是用mongo shell编写的,但是您可以使用适当的驱动程序方法从C#或任何其他语言中执行相同的操作.
note that this is written in the mongo shell, but you can do the same thing from C# or any other language using the appropriate driver methods.
这篇关于如何在mongodb中查找最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文