Mongo_mapper限制结果 [英] Mongo_mapper limit results

查看:88
本文介绍了Mongo_mapper限制结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询:

  @allJobs = Job.where(:merchant_id => session[:admin_id].to_s).sort(:start_date.desc).limit(100)

当我对此运行.count时,会得到:

When I run .count on this I get:

 jobs count 1720

我希望看到:

 jobs count 100

我在做什么错了?

如果我尝试运行Job.all(query).limit(100),则会收到数组#foo的未定义方法限制"

If I try to run Job.all(query).limit(100) I get "undefined method limit for Array #foo"

似乎没有办法将结果限制为按start_date降序排列的100条记录!

It seems there is no way I can get the results to restrict to 100 records sorted in descending order by start_date!

更新:

我什至不能做这样简单的事情:

I cannot even do something simple like this:

@allJobs = Job.limit(100)
      Rails.logger.info("@allJobs count is " + @allJobs.count.to_s)

控制台:

@allJobs count is 2080

据我所知,

.limit()完全没用.

.limit() is completely useless as far as I can tell.

推荐答案

对我来说,将子句从where开始,然后进一步缩小范围或进行修改似乎是合乎逻辑的……在MongoMapper中,我发现查询严谨性要高得多松散"而不是说SQL SELECT查询需要按正确的顺序进行操作……我倾向于以这种方式或多或少地编写我的查询:

For me it seems logical to start my clauses with the where, and narrow them down further, or modify them… In MongoMapper, I find querying rigor is much more "loose" than say a SQL SELECT query that requires things in proper order… I would tend to write my queries in more or less this fashion:

ModelClass.where(some criteria).[sort | order | another where clause | fields | limit].[all | first | paginate]

此外,需要特别注意的是,MongoMapper返回一个查询,并且在您添加需要结果的内容之前,实际上不会执行该查询.例如:allfirstpaginatesort

In addition, it is important to note that MongoMapper returns a query and does not actually perform the query until you add something that needs the results. For example: all, first, paginate, sort, etc.

2.0.0-p247 :001 > Structure.where(:address => /NJ/).count
 => 22 
2.0.0-p247 :002 > Structure.where(:address => /NJ/).limit(2).count
 => 22 
2.0.0-p247 :003 > Structure.where(:address => /NJ/).limit(2).all.count
 => 2 

此处有更多详细信息: http://technicaldebt.com/mongomapper-query-review/并获取基本的Plucky查询语法,请在此处: https://github.com/mongomapper/plucky

More details here: http://technicaldebt.com/mongomapper-query-review/ and for the underlying Plucky query syntax, here: https://github.com/mongomapper/plucky

这篇关于Mongo_mapper限制结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆