索引管理
指定索引
您可以使用索引宏在文档上定义索引。提供索引的键和方向。还可以在第二个选项哈希参数中提供其他选项
class Person include Mongoid::Document field :ssn index({ ssn: 1 }, { unique: true, name: "ssn_index" }) end
您还可以在嵌套文档字段上定义索引
class Person include Mongoid::Document embeds_many :addresses index "addresses.street" => 1 end
您可以在多个字段上索引并提供方向
class Person include Mongoid::Document field :first_name field :last_name index({ first_name: 1, last_name: 1 }, { unique: true }) end
索引可以是稀疏的
class Person include Mongoid::Document field :ssn index({ ssn: -1 }, { sparse: true }) end
对于地理空间索引,请确保被索引的字段是数组类型
class Person include Mongoid::Document field :location, type: Array index({ location: "2d" }, { min: -200, max: 200 }) end
索引可以限制在特定数据库范围内
class Person include Mongoid::Document field :ssn index({ ssn: 1 }, { database: "users", unique: true, background: true }) end
您可以在索引定义中使用别名字段名。字段别名也会在以下选项中解析partial_filter_expression
(部分筛选表达式), weights
(权重), wildcard_projection
(通配符投影)。
class Person include Mongoid::Document field :a, as: :age index({ age: 1 }, { partial_filter_expression: { age: { '$gte' => 20 } }) end
注意:
在索引选项中,如partial_filter_expression
中字段别名展开的行为是按照MongoDB 6.0服务器行为执行的。未来的服务器版本可能改变它们解释这些选项的方式,Mongoid的功能可能不支持这些更改。
Mongoid可以为关联中的“外键”字段定义索引。这仅在存储外键的关联宏上有效
class Comment include Mongoid::Document belongs_to :post, index: true has_and_belongs_to_many :preferences, index: true end
已弃用: 在MongoDB 4.0及更早版本中,用户可以使用background
选项来控制是否在后台(非阻塞但效率较低)或前台(阻塞)构建索引。
class Person include Mongoid::Document field :ssn index({ ssn: 1 }, { unique: true, background: true }) end
background
的默认值由Mongoid的background_indexing
配置选项控制.
从MongoDB 4.2开始,background
选项没有效果影响.
在MongoDB Atlas中指定搜索索引
如果你的应用程序连接到MongoDB Atlas,你可以在你的模型中声明和管理搜索索引。(此功能仅在MongoDB Atlas上可用。)
要声明搜索索引,请使用模型中的search_index
宏
class Message include Mongoid::Document search_index { ... } search_index :named_index, { ... } end
搜索索引可以赋予一个显式名称;如果模型上有多个搜索索引,则这是必要的。
索引管理Rake任务
当您想要在数据库中创建索引时,请使用提供的db:mongoid:create_indexes
Rake任务
$ rake db:mongoid:create_indexes
Mongoid还提供了一个Rake任务来删除所有辅助索引。
$ rake db:mongoid:remove_indexes
注意:这些Rake任务的输出将发送到Rails配置的默认日志记录器。这通常是一个文件,如log/development.log
,而不是标准输出。
这些创建/删除索引命令也可以通过在Rails控制台中运行来仅针对一个模型工作
# Create indexes for Model Model.create_indexes # Remove indexes for Model Model.remove_indexes
在MongoDB Atlas上管理搜索索引
如果您在模型上定义了搜索索引,则有一些rake任务可用于创建和删除这些搜索索引
$ rake db:mongoid:create_search_indexes $ rake db:mongoid:remove_search_indexes
默认情况下,创建搜索索引将等待索引创建完成,这可能需要相当长的时间。如果您只想让数据库在后台创建索引,可以将环境变量WAIT_FOR_SEARCH_INDEXES
设置为0,如下所示
$ rake WAIT_FOR_SEARCH_INDEXES=0 db:mongoid:create_search_indexes
请注意,删除搜索索引的任务将从所有模型中删除所有搜索索引,应谨慎使用。
您还可以通过在Rails控制台中执行以下操作来为单个模型添加和删除搜索索引
# Create all defined search indexes on the model; this will return # immediately and the indexes will be created in the background. Model.create_search_indexes # Remove all search indexes from the model Model.remove_search_indexes # Enumerate all search indexes on the model Model.search_indexes.each { |index| ... }
告诉Mongoid模型的位置
对于非Rails应用程序,Mongoid的rake任务将在./app/models
和./lib/models
中查找模型。对于Rails,Mongoid将在./app/models
(或您已配置Rails查找模型的任何位置)中查找。如果您的模型位于其他位置,您需要使用Mongoid.model_paths=
告诉Mongoid在哪里查找它们。您可以在应用程序的Rakefile中设置它
# Rakefile # keep the defaults, but add more paths to look for models Mongoid.model_paths += [ "./src/models", "./lib/documents" ] # or, override the defaults entirely Mongoid.model_paths = [ "./src/models", "./lib/documents" ]
确保这些路径也在您应用程序的加载路径中。例如
# Rakefile $LOAD_PATHS.concat [ "./src/models", "./lib/documents" ]
在非Rails应用程序中使用Rake任务
Mongoid的Rake任务在Rails应用程序中使用Mongoid时自动加载。当在非Rails应用程序中使用Mongoid时,必须手动加载这些任务
# Rakefile require 'mongoid' load 'mongoid/tasks/database.rake'
如果您的应用程序使用Bundler,您可以require bundler/setup
而不是显式require mongoid
# Rakefile require 'bundler/setup' load 'mongoid/tasks/database.rake'