Skip to content

Commit 3f75ae0

Browse files
committed
Edits
1 parent 86323df commit 3f75ae0

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

source/includes/reference/indexes.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Restaurant
2323
# end create alias index
2424

2525
# start create embedded index
26+
class Address
27+
include Mongoid::Document
28+
29+
field :street, type: String
30+
end
31+
2632
class Restaurant
2733
include Mongoid::Document
2834

@@ -38,15 +44,15 @@ class Restaurant
3844
field :name, type: String
3945
embeds_many :addresses
4046

41-
index({"addresses.street": 1, name: -1}, { name: "compound_index"})
47+
index({borough: 1, name: -1}, { name: "compound_index"})
4248
end
4349
# end create compound index
4450

4551
# start create 2dsphere index
4652
class Restaurant
4753
include Mongoid::Document
4854

49-
field :location, type: Point
55+
field :location, type: Array
5056

5157
index({location: "2dsphere"}, { name: "location_index"})
5258
end
@@ -64,10 +70,6 @@ class Restaurant
6470
end
6571
# end create sparse index
6672

67-
# start list index
68-
Restaurant.indexes.each { |index| puts index }
69-
# end list index
70-
7173
# start create multiple indexes
7274
class Restaurant
7375
include Mongoid::Document
@@ -95,7 +97,18 @@ class Restaurant
9597
field :cuisine, type: String
9698
field :borough, type: String
9799

98-
search_index({ name: 1})
100+
search_index :my_search_index,
101+
mappings: {
102+
fields: {
103+
name: {
104+
type: "string"
105+
},
106+
cuisine: {
107+
type: "string"
108+
}
109+
},
110+
dynamic: true
111+
}
99112
end
100113

101114
Restaurant.create_search_indexes

source/reference/indexes.txt

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Work With Indexes
99
.. contents:: On this page
1010
:local:
1111
:backlinks: none
12-
:depth: 2
12+
:depth: 3
1313
:class: singlecol
1414

1515
Overview
@@ -44,9 +44,12 @@ named ``cuisine_index`` on the ``cuisine`` field in the ``Restaurant`` class:
4444
:end-before: end create index
4545

4646
The ``index`` macro defines the index you want to create and the ``create_index``
47-
macro creates the index in the ``restaurants`` collection. When defining an
48-
index, the field you want to index on goes in the first hash object, and the
49-
index options go in the second hash object.
47+
macro creates the index in the ``restaurants`` collection.
48+
49+
When defining an index, the first hash object contains the field you want to
50+
index and its direction. ``1`` represents an ascending index, ``-1`` represents a
51+
descending index. The second hash object contains index options. To learn more
52+
about index options, see the :ref:`mongoid_indexes_api_documentation` section.
5053

5154
Aliases and Declaring Indexes
5255
+++++++++++++++++++++++++++++
@@ -98,10 +101,10 @@ objects:
98101
:start-after: start create 2dsphere index
99102
:end-before: end create 2dsphere index
100103

101-
For more information on 2dsphere indexes, see the :manual:`2dsphere <core/indexes/index-types/geospatial/2dsphere/>`
104+
For more information on 2dsphere indexes, see the :manual:`2dsphere </core/indexes/index-types/geospatial/2dsphere/>`
102105
guide in the {+mdb-server+} manual.
103106

104-
For more information on the GeoJSON type, see the :manual:`GeoJSON Objects <reference/geojson/>`
107+
For more information on the GeoJSON type, see the :manual:`GeoJSON Objects </reference/geojson/>`
105108
guide in the {+mdb-server+} manual.
106109

107110
Create a Sparse Index
@@ -115,7 +118,7 @@ The following code example defines a sparse index on the ``borough`` field:
115118
:start-after: start create sparse index
116119
:end-before: end create sparse index
117120

118-
For more information on sparse indexes, see the :manual:`Sparse Indexes <core/index-sparse/>`
121+
For more information on sparse indexes, see the :manual:`Sparse Indexes </core/index-sparse/>`
119122
guide in the {+mdb-server+} manual.
120123

121124
Create Multiple Indexes
@@ -130,21 +133,6 @@ indexes at the same time:
130133
:start-after: start create multiple indexes
131134
:end-before: end create multiple indexes
132135

133-
.. TODO checkout what databases does
134-
135-
List Indexes
136-
~~~~~~~~~~~~
137-
138-
You can list all indexes in your collection. The following example prints all
139-
indexes in the ``Restaurant`` model:
140-
141-
.. literalinclude:: /includes/reference/indexes.rb
142-
:language: ruby
143-
:start-after: start list index
144-
:end-before: end list index
145-
146-
.. TODO test if there's a way to drop single index
147-
148136
Drop Indexes
149137
~~~~~~~~~~~~
150138

@@ -156,21 +144,32 @@ indexes in the ``Restaurant`` model:
156144
:start-after: start drop indexes
157145
:end-before: end drop indexes
158146

159-
.. TODO: Is there a way to only drop 1
147+
.. note:: Default Index
148+
149+
MongoDB creates a default index on the ``_id`` field during the
150+
creation of a collection. This index prevents clients from inserting
151+
two documents with the same values for the ``_id`` field. You cannot
152+
drop this index.
160153

161154
Atlas Search Indexes
162155
~~~~~~~~~~~~~~~~~~~~
163156

164157
You can declare and manage Atlas Search indexes using {+odm+}.
165158

166159
To declare a search index, use the ``search_index`` macro within your model. The
167-
following code example shows how to declare and create an Atlas Search index:
160+
following code example shows how to declare and create an Atlas Search index
161+
named ``my_search_index``.
162+
The index is on the ``name`` and ``cuisine`` fields and is dynamic.
168163

169164
.. literalinclude:: /includes/reference/indexes.rb
170165
:language: ruby
171166
:start-after: start create atlas search index
172167
:end-before: end create atlas search index
173168

169+
To learn more about the syntax for creating an Atlas Search index, see
170+
the :atlas:`Create an Atlas Search Index </atlas-search/create-index/#std-label-ref-create-index>`
171+
guide in the MongoDB Atlas documentation.
172+
174173
Remove an Atlas Search Index
175174
++++++++++++++++++++++++++++
176175

@@ -186,14 +185,28 @@ following code example shows how to remove an Atlas Search index from the
186185
List Atlas Search Indexes
187186
+++++++++++++++++++++++++
188187

189-
You can list all Atlas Search indexes in your collection by using the
190-
``search_indexes`` macro:
188+
You can enumerate through all Atlas Search indexes in your collection
189+
by using the ``search_indexes`` macro. The following example enumerates through
190+
all Atlas Search indexes in the ``restaurants`` collection and prints out their
191+
information:
191192

192193
.. literalinclude:: /includes/reference/indexes.rb
194+
:language: ruby
195+
:start-after: start list atlas search index
196+
:end-before: end list atlas search index
197+
198+
.. _mongoid-indexes-api-documentation:
193199

194200
API Documentation
195201
-----------------
196202

197203
To learn more about using indexes in {+odm+}, see the
198204
`Mongoid::Indexable::ClassMethods <{+api-root+}/Indexable/ClassMethods.html>`__
199-
guide.
205+
documentation.
206+
207+
To learn more about index options, see the `Mongoid::Indexable::Validators::Options
208+
<{+api-root+}/Indexable/Validators/Options.html>`__ documentation.
209+
210+
To learn more about using Atlas Search indexes in {+odm+}, see the
211+
`Mongoid::SearchIndexable::ClassMethods <{+api-root+}/SearchIndexable/ClassMethods.html>`__
212+
documentation.

0 commit comments

Comments
 (0)