Docs 菜单
Docs 主页
/ / /
Ruby 驱动程序

使用索引优化查询

在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用MongoDB Ruby驱动程序管理不同类型的索引。

要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <connection string>)替换为MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试此页面上的代码。 要使用示例应用程序,请执行以下步骤:

  1. 确保您已在项目中安装Ruby驾驶员。请参阅 下载和安装指南以学习;了解详情。

  2. 复制以下代码并将其粘贴到新的.rb文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

require 'mongo'
# Replace the placeholders with your credentials
uri = "<connection string>"
# Sets the server_api field of the options object to Stable API version 1
options = { server_api: { version: "1" }}
# Creates a new client and connect to the server
client = Mongo::Client.new(uri, options)
database = client.use('<database name>')
collection = database[:<collection name>]
# Start example code here
# End example code here
client.close

以下示例在指定字段上创建一个升序索引:

collection.indexes.create_one({ <field name>: 1 })

要学习;了解有关单字段索引的更多信息,请参阅单字段索引指南。

以下示例在两个指定字段上创建复合索引。

collection.indexes.create_one({ <field name 1>: -1, <field name 2>: 1 })

要了解有关复合索引的更多信息,请参阅复合索引指南。

以下示例在指定的数组值字段上创建多键索引:

collection.indexes.create_one({ <field name>: 1 })

要了解有关多键索引的更多信息,请参阅多键索引指南。

以下示例在包含GeoJSON对象的指定字段上创建2 dsphere索引:

collection.indexes.create_one({ <GeoJSON field name>: '2dsphere' })

要了解有关地理空间索引的更多信息,请参阅地理空间索引指南。

以下部分包含描述如何管理Atlas Search索引的代码示例。

要学习;了解有关搜索索引的更多信息,请参阅Atlas Search索引指南

以下示例在指定字段上创建Atlas Search索引:

index_definition = {
mappings: {
dynamic: false,
fields: {
<field name>: { type: '<field type>' }
}
}
}
collection.search_indexes.create_one(index_definition, name: '<index name>')

以下示例将打印指定集合中的Atlas Search索引列表:

puts collection.search_indexes.collect(&:to_json)

以下示例使用指定的新索引定义更新现有Atlas Search索引:

updated_definition = {
mappings: {
dynamic: false,
fields: { <updated field name>: { type: '<updated field type>' } }
}
}
collection.search_indexes.update_one(updated_definition, name: '<index name>')

以下示例删除具有指定名称的Atlas Search索引:

collection.search_indexes.drop_one(name: '<index name>')

以下示例在指定string字段上创建文本索引:

collection.indexes.create_one({ <field name>: 'text' })

要了解有关文本索引的更多信息,请参阅文本索引指南。

以下示例在给定的索引规范大量上创建多个索引:

collection.indexes.create_many([
{ key: { <field name 1>: 1 } },
{ key: { <field name 2>: -1 } },
])

以下示例删除具有指定名称的索引:

collection.indexes.drop_one( '<index name>' )

以下示例演示如何删除集合中的所有索引:

collection.indexes.drop_all

以下示例打印指定集合中所有索引的列表:

puts collection.indexes.collect(&:to_json)

以下是创建索引时可以添加的可用选项的完整列表。这些选项反映了createIndex 命令支持的选项。有关更多信息,请参阅MongoDB Server手册中的 createIndex 命令。

选项
说明

:background

truefalse 。 指示在背景创建索引。

:expire_after

集合中的文档在此时间后过期的秒数。

:name

索引的名称。

:sparse

索引是否应该稀疏,可以是truefalse

:storage_engine

此特定索引的storage engine的名称。

:version

要使用的索引格式版本。

:default_language

文本索引的默认语言。

:language_override

覆盖默认语言时要使用的字段名称。

:text_version

文本索引存储的版本格式。

:weights

在文本搜索中指定字段和权重的文档。

:sphere_version

2d 球体索引版本。

:bits

设置 2d 索引中纬度和经度的最大边界。

:max

2d 索引中纬度和经度的最大边界。

:min

2d 索引中纬度和经度的最小边界。

:bucket_size

对地理干草堆索引中的位置值进行分组的单位数。

:partial_filter_expression

部分索引的筛选器。

:hidden

一个布尔值,指定是否应隐藏索引;隐藏索引是指集合中存在但查询规划器不会使用的索引。

:commit-quorum

指定在主节点 (primary node in the replica set)节点将索引标记为就绪之前,副本集的承载数据的节点数量(包括主节点 (primary node in the replica set))必须成功完成索引构建。 潜在值为:

  • 从 0 到副本集点数的整数

  • “majority” 表示大多数数据承载节点必须投票

  • “votingMembers” 这意味着所有承载投票数据的节点必须投票

有关更多信息,请参阅MongoDB Server手册中的 commitQuorom。

要学习;了解有关本指南中使用的方法或对象的更多信息,请参阅以下API文档:

后退

对副本集的操作

在此页面上