索引管理
在此页面上
指定索引
您可以使用索引宏在文档上定义索引。提供索引的键和方向。可以在第二个选项哈希参数中提供其他选项:
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 开始, background
选项对 影响。 2 。
在 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 任务的输出将 Go 到 Rails 配置的默认记录器。这通常是类似log/development.log
的文件,而不是标准输出。
通过在 Rails 控制台中运行,这些创建/删除索引命令也仅适用于一种模型:
# Create indexes for Model Model.create_indexes # Remove indexes for Model Model.remove_indexes
管理 上的Atlas Search 索引MongoDB Atlas
如果您在模型上定义了Atlas Search索引,则可以使用 rake 任务来创建和删除这些Atlas Search索引:
$ rake db:mongoid:create_search_indexes $ rake db:mongoid:remove_search_indexes
默认情况下,创建Atlas Search索引时将等待索引创建完成,这可能需要相当长的时间。 如果只想让数据库在后台创建索引,可以将WAIT_FOR_SEARCH_INDEXES
环境变量设置为0 ,如下所示:
$ rake WAIT_FOR_SEARCH_INDEXES=0 db:mongoid:create_search_indexes
请注意,删除Atlas Search索引的任务将从所有模型中删除所有Atlas Search索引,应谨慎使用。
您还可以通过在 Rails 控制台中调用以下命令来添加和删除单个模型的Atlas Search索引:
# 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 任务会自动加载到使用 Mongoid 的 Rails 应用程序中。将 Mongoid 与非 Rails 应用程序一起使用时,必须手动加载这些任务:
# Rakefile require 'mongoid' load 'mongoid/tasks/database.rake'
如果您的应用程序使用 Bundler,您可以要求bundler/setup
而不是显式要求mongoid
:
# Rakefile require 'bundler/setup' load 'mongoid/tasks/database.rake'