Specify elements in MongoDB according to array size with MongoMapper?

I would like to select a set of ordered elements based on the number of elements in the array. Hopefully the following example will clarify my rather poor explanation:

class Thing
  include MongoMapper::Document

  key :name, String
  key :tags, Array
end

I would like to get everything Thingordered from those who have more tags, with the smallest. The tags in this example are just strings in the tag array. Basically I want something that means the same thing as this (but works):

Thing.all(:order => 'tags.count desc')

Is it possible?

+3
source share
1 answer

, . , .

class Thing
  include MongoMapper::Document

  key :name,     String
  key :tags,     Array
  key :tag_size, Integer, :default => 0, :index => true
end

, tag_size .

, , :

http://jira.mongodb.org/browse/SERVER

+3

Source: https://habr.com/ru/post/1729763/


All Articles