Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo_mapper limit results

I have a query like this:

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

When I run .count on this I get:

 jobs count 1720

I expect to see :

 jobs count 100

What am I doing wrong?

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

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

UPDATE:

I cannot even do something simple like this:

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

Console:

@allJobs count is 2080

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

like image 445
notaceo Avatar asked Nov 17 '25 22:11

notaceo


1 Answers

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]

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 

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

like image 102
Jon Kern Avatar answered Nov 19 '25 12:11

Jon Kern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!