How to sort the Mongoid model by the length of the array which is a field inside the model.
相关问题
- MongoDB can not create unique sparse index (duplic
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- Right way to deploy Rails + Puma + Postgres app to
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
In ruby, you can sort an array like this :
It will sort the array
my_array
by the attributemy_attr
of each element inside the array.You can also write it like this :
Which is exactly the same, it will sort by the
my_attr
attribute of each element. This second syntax is for when you want a more complex sort condition than just the result of a method of each element.Documentation : http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-sort_by
Mongo documentation says:
So we cannot order by using mongo's
$size
.You can solve your task by adding new field, which will store array size.
Sort
posts
bylikes_size
:Document says that you can't orderby using size.
Try adding a new column containing the value of size and sort it which will work as order by.