Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 获取MongoDB中列的最大值_Ruby_Mongodb_Mongodb Ruby - Fatal编程技术网

Ruby 获取MongoDB中列的最大值

Ruby 获取MongoDB中列的最大值,ruby,mongodb,mongodb-ruby,Ruby,Mongodb,Mongodb Ruby,我一直在为mongo文档获取列的最高值寻求帮助。我可以对它进行排序并得到顶部/底部,但我很确定有更好的方法 我尝试了以下(以及不同的组合): 但它不断抛出错误。除了排序和获取顶部/底部之外,还有什么好方法 谢谢大家! max()的工作方式与您在SQL for Mongo中预期的不同。这可能会在未来的版本中发生变化,但到目前为止,max、min将主要用于内部切分的索引键 看 不幸的是,现在获取最大值的唯一方法是根据该值对集合desc进行排序,然后取第一个值 transactions.find("i

我一直在为mongo文档获取列的最高值寻求帮助。我可以对它进行排序并得到顶部/底部,但我很确定有更好的方法

我尝试了以下(以及不同的组合):

但它不断抛出错误。除了排序和获取顶部/底部之外,还有什么好方法

谢谢大家!

max()的工作方式与您在SQL for Mongo中预期的不同。这可能会在未来的版本中发生变化,但到目前为止,max、min将主要用于内部切分的索引键

不幸的是,现在获取最大值的唯一方法是根据该值对集合desc进行排序,然后取第一个值

transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()

如果列已被索引,那么排序应该是可以的,假设Mongo只使用索引来获取有序集合。否则,对集合进行迭代会更有效,并记录所看到的最大值。e、 g

max = nil
coll.find("id" => x).each do |doc| 
    if max == nil or doc['sellprice'] > max then
        max = doc['sellprice'] 
    end
end

(如果我的Ruby有点不合适,我很抱歉,我已经很久没有使用它了-但是一般的方法应该从代码中很清楚。)

假设我使用的是Ruby驱动程序(我在底部看到了mongodb Ruby标记),如果我想获得最大的
\u id
(假设我的
\u id
是可排序的),我会做如下的事情。在我的实现中,我的
\u id
是一个整数

result = my_collection.find({}, :sort => ['_id', :desc]).limit(1)

要获取集合中的最小
\u id
,只需将
:desc
更改为
:asc

排序可能会有些过分。你可以一组一组地做

db.messages.group(
           {key: { created_at:true },
            cond: { active:1 },
            reduce: function(obj,prev) { if(prev.cmax<obj.created_at) prev.cmax = obj.created_at; },
            initial: { cmax: **any one value** }
            });
db.messages.group(
{key:{created_at:true},
条件:{活动:1},
reduce:function(obj,prev){if(prev.cmaxUse aggregate():


用于计算聚合的示例mongodb外壳代码

请参阅mongodb手动输入组(许多应用程序)::

在下面的示例中,用集合键和目标变量替换$vars

db.activity.aggregate( 
  { $group : {
      _id:"$your_collection_key", 
      min: {$min : "$your_target_variable"}, 
      max: {$max : "$your_target_variable"}
    }
  } 
)

以下查询执行相同的操作:
db.student.find({},{u-id:1}).sort({u-id:-1}).limit(1)

对我来说,这产生了以下结果:
{“\u id”:NumberLong(10934)}

它将根据您的要求工作

transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()

你应该包括它正在抛出的错误。考虑使用MapReduce迭代!!这就是为什么MangGDB是很酷的,否则使用平面文件;-----难道找不到比使用LimIT()更好的方法吗?<代码>结果= MyOrthCyto.FixOne({},排序=> [SyId,,DISC])< /C> >我不会说这是一个过度的考虑。“当一个$sort紧跟在管道中的$limit之前时,$sort操作只会在执行过程中保留前n个结果,其中n是指定的限制”这个答案的意义是什么?重复接受的答案?
db.collectionName.aggregate(
  {
    $group : 
    {
      _id  : "",
      last : 
      {
        $max : "$sellprice"
      }
    }
  }
)
db.activity.aggregate( 
  { $group : {
      _id:"$your_collection_key", 
      min: {$min : "$your_target_variable"}, 
      max: {$max : "$your_target_variable"}
    }
  } 
)
transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()