-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45205 Multikey Indexes #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
00794c9
1ef45ef
3184be0
f5abd5f
48e9326
6d52f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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) | ||
|
||
# start-sample-data | ||
database = client.use('sample_mflix') | ||
collection = database[:movies] | ||
# end-sample-data | ||
|
||
# start-index-multikey | ||
# Creates an index on the "cast" field | ||
collection.indexes.create_one({ cast: 1 }) | ||
# end-index-multikey | ||
|
||
# start-index-multikey-query | ||
# Finds a document with the specified cast members by using the newly created index | ||
filter = { cast: { '$all' => ['Aamir Khan', 'Kajol'] } } | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
doc = collection.find(filter).first | ||
|
||
if doc | ||
puts doc.to_json | ||
else | ||
puts "No document found" | ||
end | ||
# end-index-multikey-query | ||
|
||
# start-check-multikey-index | ||
# Lists all indexes on the collection | ||
puts collection.indexes.collect(&:to_json) | ||
# end-check-multikey-index |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
.. _ruby-multikey-index: | ||
|
||
================ | ||
Multikey Indexes | ||
================ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: index, query, optimization, efficiency | ||
|
||
Overview | ||
-------- | ||
|
||
**Multikey indexes** are indexes that improve the performance of queries | ||
on array-valued fields. You can create a multikey index on a collection | ||
by using the ``create_one`` method and the same syntax that you use to create | ||
a :ref:`single field index <ruby-single-field-index>`. | ||
|
||
|
||
When creating a multikey index, you must specify the following details: | ||
|
||
- The fields on which to create the index | ||
|
||
- The sort order for each field (ascending or descending) | ||
|
||
Sample Data | ||
~~~~~~~~~~~ | ||
|
||
The examples in this guide use the ``movies`` collection in the | ||
``sample_mflix`` database from the :atlas:`Atlas sample datasets | ||
</sample-data>`. To access this collection from your {+language+} | ||
application, create a ``Mongo::Client`` object that connects to | ||
an Atlas cluster and assign the following values to your ``database`` | ||
and ``collection`` variables: | ||
|
||
.. literalinclude:: /includes/indexes/single-field.rb | ||
:start-after: start-sample-data | ||
:end-before: end-sample-data | ||
:language: ruby | ||
:copyable: | ||
|
||
To learn how to create a free MongoDB Atlas cluster and | ||
load the sample datasets, see the :atlas:`Get Started with Atlas | ||
</getting-started>` guide. | ||
|
||
Create a Multikey Index | ||
----------------------- | ||
|
||
Use the ``create_one`` method to create a multikey index. The following example | ||
creates an index in ascending order on the ``cast`` field: | ||
|
||
.. literalinclude:: /includes/indexes/multikey.rb | ||
:start-after: start-index-multikey | ||
:end-before: end-index-multikey | ||
:language: ruby | ||
:copyable: | ||
|
||
Verify Index Creation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same feedback on this section as in the compound index review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcmorisi Commented on the compound index PR for my reasoning to keep it. Let me know what you think! |
||
--------------------- | ||
|
||
You can verify that the index was created by listing the indexes in the | ||
collection. You should see an index for ``cast`` in the list, as shown | ||
in the following output: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/indexes/multikey.rb | ||
:start-after: start-check-multikey-index | ||
:end-before: end-check-multikey-index | ||
:language: ruby | ||
|
||
.. output:: | ||
:visible: true | ||
|
||
{"v": 2, "key": {"cast": 1}, "name": "cast_1"} | ||
|
||
Example Query | ||
------------- | ||
|
||
The following is an example of a query that is covered by the index | ||
created on the ``cast`` field: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/indexes/multikey.rb | ||
:start-after: start-index-multikey-query | ||
:end-before: end-index-multikey-query | ||
:language: ruby | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
{"_id":...,"title":"Fanaa",...,"cast": ["Aamir Khan", "Kajol", "Rishi Kapoor", "Tabu"],...} | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To view runnable examples that demonstrate how to manage indexes, see | ||
:ref:`ruby-indexes`. | ||
|
||
To learn more about multikey indexes, see :manual:`Multikey | ||
Indexes </core/indexes/index-types/index-multikey/>` in the {+mdb-server+} manual. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about any of the methods discussed in this guide, see the | ||
following API documentation: | ||
|
||
- `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__ | ||
- `create_one <{+api-root+}/Mongo/Index/View.html>`__ | ||
- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
Uh oh!
There was an error while loading. Please reload this page.