-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45204 Compound Index #106
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 5 commits
f28a372
fdd4dc9
cffb088
13917aa
3d55188
72778be
241a5e2
3cd16f2
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,41 @@ | ||
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-compound | ||
# Creates an index on the "runtime" and "year" field | ||
collection.indexes.create_one({ runtime: -1, year: 1 }) | ||
# end-index-compound | ||
|
||
# start-index-compound-query | ||
# Finds a document with the specified runtime and release year by using the | ||
# newly created index | ||
filter = { '$and' => [ | ||
{ 'runtime': { '$gt' => 90 } }, | ||
{ 'year': { '$gt' => 2005 } } | ||
] } | ||
doc = collection.find(filter).first | ||
|
||
if doc | ||
puts doc.to_json | ||
else | ||
puts "No document found" | ||
end | ||
# end-index-compound-query | ||
|
||
# start-check-compound-index | ||
# Lists all indexes on the collection | ||
puts collection.indexes.collect(&:to_json) | ||
# end-check-compound-index |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
.. _ruby-compound-index: | ||
|
||
================ | ||
Compound Indexes | ||
================ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: index, query, optimization, efficiency | ||
|
||
Overview | ||
-------- | ||
|
||
**Compound indexes** hold references to multiple | ||
fields within a collection's documents, improving query and sort | ||
performance. | ||
|
||
When creating a compound 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 Compound Index | ||
----------------------- | ||
|
||
Use the ``create_one`` method to create a compound index. The following | ||
example creates an index in descending order on the ``runtime`` field and | ||
in ascending order on the ``year`` field: | ||
|
||
.. literalinclude:: /includes/indexes/compound.rb | ||
:start-after: start-index-compound | ||
:end-before: end-index-compound | ||
:language: ruby | ||
:copyable: | ||
|
||
Verify Index Creation | ||
--------------------- | ||
|
||
Comment on lines
+67
to
+69
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. I feel like it's somewhat repetitive to include this section for every page for individual index types. I personally think it would suffice to include this code sample once in the top-level Indexes page. 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 Tried out linking to the Listing Indexes section on the overview page, but then users won't be able to see the output (as it doesn't seem to be our standard to show output on the overview page). The section is relatively small, so it doesn't make the page significantly longer. I also think there is a use case where each index page will be viewed independently of the others, depending on which index the user wants to create. What do you think about keeping it? |
||
You can verify that the index was created by listing the indexes in the | ||
collection. You should see an index for ``runtime`` and ``year`` in the list, | ||
as shown in the following output: | ||
|
||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/indexes/compound.rb | ||
:start-after: start-check-compound-index | ||
:end-before: end-check-compound-index | ||
:language: ruby | ||
|
||
.. output:: | ||
:visible: true | ||
|
||
{"v": 2, "key": {"runtime": -1, "year": 1}, "name": "runtime_-1_year_1"} | ||
|
||
Example Query | ||
------------- | ||
|
||
The following is an example of a query that is covered by the index | ||
created on the ``runtime`` and ``year`` fields: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/indexes/compound.rb | ||
:start-after: start-index-compound-query | ||
:end-before: end-index-compound-query | ||
:language: ruby | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
{"_id":...,"runtime": 91,...,"title": "Monster House",...,"year": 2006,...} | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To view runnable examples that demonstrate how to manage indexes, see | ||
:ref:`ruby-indexes`. | ||
|
||
To learn more about compound indexes, see :manual:`Compound | ||
Indexes </core/index-compound>` 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#create_one-instance_method>`__ | ||
- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using the colon syntax for hash keys, you can generally omit the quotes: