解决方法
现在,我使用了一种非常简单的方法,它非常有效:只需包含一个数组字段.
#app/models/image.rb class Image include Mongoid::Document include Mongoid::Timestamps field :message,:type => String field :tags,:type => Array def self.images_for tag Image.any_in(:tags => [tag]) end end #routes.rb match "tag/:tag" => "images#tag" #/app/controller/images_controller.rb class ImagesController < ApplicationController # GET /tag # GET /tag.xml def tag @images = Image.images_for params[:tag] respond_to do |format| format.html { render "index" } format.xml { render :xml => @images } end end end
这有效,但我仍然对Image.any_in map / reduce的性能有点怀疑.我认为可能有一个更好的解决方案,但尚未找到它.