From 00794c97d2a20f9dea56690dd2d528ed62ed407a Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 13 Dec 2024 18:38:02 -0500 Subject: [PATCH 1/5] DOCSP-45205 Multikey Indexes --- source/indexes.txt | 5 +++-- source/indexes/multikey-index.txt | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 source/indexes/multikey-index.txt diff --git a/source/indexes.txt b/source/indexes.txt index 454bb8fd..398df50b 100644 --- a/source/indexes.txt +++ b/source/indexes.txt @@ -21,12 +21,13 @@ Optimize Queries by Using Indexes .. toctree:: :titlesonly: :maxdepth: 1 - + Single Field + Multikey .. Compound -.. Multikey .. Atlas Search + Overview -------- diff --git a/source/indexes/multikey-index.txt b/source/indexes/multikey-index.txt new file mode 100644 index 00000000..48e62ae7 --- /dev/null +++ b/source/indexes/multikey-index.txt @@ -0,0 +1,27 @@ +.. _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 `. + From 1ef45ef510423c6ecefb7ec880149aafb3851c29 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Mon, 16 Dec 2024 18:10:47 -0500 Subject: [PATCH 2/5] first draft --- source/includes/indexes/multikey.rb | 37 +++++++++++ source/indexes/multikey-index.txt | 97 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 source/includes/indexes/multikey.rb diff --git a/source/includes/indexes/multikey.rb b/source/includes/indexes/multikey.rb new file mode 100644 index 00000000..1489197b --- /dev/null +++ b/source/includes/indexes/multikey.rb @@ -0,0 +1,37 @@ +require 'mongo' + +# Replace the placeholders with your credentials +uri = "" + +# 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 "title" 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'] } } +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 diff --git a/source/indexes/multikey-index.txt b/source/indexes/multikey-index.txt index 48e62ae7..3c220dcd 100644 --- a/source/indexes/multikey-index.txt +++ b/source/indexes/multikey-index.txt @@ -25,3 +25,100 @@ 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 `. + +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 +`. 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 +` 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 +--------------------- + +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 ` 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>`__ From 3184be0c9d8e22ba0a8cbc6d13bf65a8e2ab3ee0 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Mon, 16 Dec 2024 18:17:32 -0500 Subject: [PATCH 3/5] edits --- source/includes/indexes/multikey.rb | 2 +- source/indexes/multikey-index.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/source/includes/indexes/multikey.rb b/source/includes/indexes/multikey.rb index 1489197b..e9827419 100644 --- a/source/includes/indexes/multikey.rb +++ b/source/includes/indexes/multikey.rb @@ -15,7 +15,7 @@ # end-sample-data # start-index-multikey -# Creates an index on the "title" field +# Creates an index on the "cast" field collection.indexes.create_one({ cast: 1 }) # end-index-multikey diff --git a/source/indexes/multikey-index.txt b/source/indexes/multikey-index.txt index 3c220dcd..f394b520 100644 --- a/source/indexes/multikey-index.txt +++ b/source/indexes/multikey-index.txt @@ -40,8 +40,7 @@ The examples in this guide use the ``movies`` collection in the `. 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: +and ``collection`` variables: .. literalinclude:: /includes/indexes/single-field.rb :start-after: start-sample-data From f5abd5fd8735d000ca462053e8df89d3e2b0967d Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Tue, 17 Dec 2024 11:57:00 -0500 Subject: [PATCH 4/5] remove quotes from key name --- source/includes/indexes/multikey.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/includes/indexes/multikey.rb b/source/includes/indexes/multikey.rb index e9827419..acd3471d 100644 --- a/source/includes/indexes/multikey.rb +++ b/source/includes/indexes/multikey.rb @@ -21,7 +21,7 @@ # start-index-multikey-query # Finds a document with the specified cast members by using the newly created index -filter = { 'cast': { '$all' => ['Aamir Khan', 'Kajol'] } } +filter = { cast: { '$all' => ['Aamir Khan', 'Kajol'] } } doc = collection.find(filter).first if doc From 48e93267771201b0be4a94ba48b04b1110a3ee20 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Thu, 19 Dec 2024 17:53:55 -0500 Subject: [PATCH 5/5] remove comments from code snippets --- source/includes/indexes/multikey.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/includes/indexes/multikey.rb b/source/includes/indexes/multikey.rb index acd3471d..4c4b08fa 100644 --- a/source/includes/indexes/multikey.rb +++ b/source/includes/indexes/multikey.rb @@ -14,13 +14,13 @@ collection = database[:movies] # end-sample-data -# start-index-multikey # Creates an index on the "cast" field +# start-index-multikey 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 +# start-index-multikey-query filter = { cast: { '$all' => ['Aamir Khan', 'Kajol'] } } doc = collection.find(filter).first @@ -31,7 +31,7 @@ end # end-index-multikey-query -# start-check-multikey-index # Lists all indexes on the collection +# start-check-multikey-index puts collection.indexes.collect(&:to_json) # end-check-multikey-index