Skip to content

Commit 6780c89

Browse files
DOCSP-9588 builders indexes (#64)
* added builders indexes
1 parent 1386f49 commit 6780c89

File tree

3 files changed

+460
-0
lines changed

3 files changed

+460
-0
lines changed

source/fundamentals/builders/indexes.txt

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,181 @@ Indexes Builders
1111
:class: singlecol
1212

1313
.. _indexes-builders:
14+
15+
Overview
16+
--------
17+
18+
This guide shows you how to specify **indexes** using the Java Driver
19+
:doc:`builders </fundamentals/builders/>`. Index builders provide
20+
helper methods for the following types of indexes:
21+
22+
- :ref:`Ascending Indexes <ascending-indexes>`
23+
- :ref:`Descending Indexes <descending-indexes>`
24+
- :ref:`Compound Indexes <index-compound-indexes>`
25+
- :ref:`Text Indexes <index-text-indexes>`
26+
- :ref:`Hashed Indexes <index-hashed-indexes>`
27+
- :ref:`Geospatial Indexes <index-geospatial-indexes>`
28+
29+
Indexes store a subset of the collection’s data set. The index stores
30+
the value of a specific field or set of fields, ordered by the value of
31+
the field. See our guide on :doc:`Indexes </fundamentals/indexes>` for
32+
examples of queries covered by indexes.
33+
34+
The :java-core-api:`Indexes <com/mongodb/client/model/Indexes.html>`
35+
class provides static factory methods for all the MongoDB index types.
36+
Each method returns a :java-docs:`Bson <apidocs/bson/org/bson/conversions/Bson.html>`
37+
instance, which you can pass to
38+
:java-sync-api:`createIndex() <com/mongodb/client/MongoCollection.html#createIndex(org.bson.conversions.Bson)>`.
39+
40+
.. tip::
41+
42+
For brevity, you may choose to import all methods of the
43+
:java-core-api:`Indexes <com/mongodb/client/model/Indexes.html>`
44+
class statically:
45+
46+
.. code-block:: java
47+
48+
import static com.mongodb.client.model.Indexes.*;
49+
50+
The examples below assume this static import.
51+
52+
.. _ascending-indexes:
53+
54+
Ascending Indexes
55+
-----------------
56+
57+
An ascending index enables you to sort query results by the value of the
58+
indexed field(s) from smallest to largest.
59+
60+
In order to create an ascending index, first call the
61+
:java-core-api:`ascending() <com/mongodb/client/model/Indexes.html#ascending(java.lang.String...)>`
62+
builder method to create a ``Bson`` instance that represents the index
63+
document, passing the name or names of the fields you want to index.
64+
Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
65+
instance that contains the index document.
66+
67+
.. note::
68+
69+
If you have an ascending or a descending index on a single field, MongoDB
70+
can sort using the index in either direction.
71+
72+
The following example specifies an ascending index on the ``name`` field:
73+
74+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
75+
:language: java
76+
:dedent:
77+
:start-after: begin ascendingIndex
78+
:end-before: end ascendingIndex
79+
80+
.. _descending-indexes:
81+
82+
Descending Indexes
83+
------------------
84+
85+
A descending index enables you to sort query results by the value of the
86+
indexed field(s) from largest to smallest.
87+
88+
In order to create a descending index, first call the
89+
:java-core-api:`descending() <com/mongodb/client/model/Indexes.html#descending(java.lang.String...)>`
90+
builder method to create a ``Bson`` instance that represents the index
91+
document, passing the name or names of the fields you want to index.
92+
Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
93+
instance that contains the index document.
94+
95+
The following example specifies a descending index on the ``capacity`` field:
96+
97+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
98+
:language: java
99+
:dedent:
100+
:start-after: begin descendingIndex
101+
:end-before: end descendingIndex
102+
103+
.. _index-compound-indexes:
104+
105+
Compound Indexes
106+
----------------
107+
108+
In order to create a compound index, first call the
109+
:java-core-api:`compoundIndex() <com/mongodb/client/model/Indexes.html#compoundIndex(org.bson.conversions.Bson...)>`
110+
builder method to create a ``Bson`` instance that represents the index
111+
document, passing the names of the fields you want to index. Then, call
112+
the ``createIndex()`` method on the collection, passing the ``Bson``
113+
instance that contains the index document.
114+
115+
The following example specifies a compound index composed of
116+
descending index on the ``capacity`` and ``year`` field, followed
117+
by an ascending index on the ``name`` field:
118+
119+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
120+
:language: java
121+
:dedent:
122+
:start-after: begin compoundIndexExample
123+
:end-before: end compoundIndexExample
124+
125+
.. _index-text-indexes:
126+
127+
Text Indexes
128+
------------
129+
130+
A text index groups documents by the text in the indexed field.
131+
132+
In order to create a text index, first call the
133+
:java-core-api:`text() <com/mongodb/client/model/Indexes.html#text(java.lang.String)>`
134+
builder method to create a ``Bson`` instance that represents the index
135+
document, passing the name of the fields you want to index. Then, call
136+
the ``createIndex()`` method on the collection, passing the ``Bson``
137+
instance that contains the index document.
138+
139+
The following example specifies a text index key on the "theaters" field:
140+
141+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
142+
:language: java
143+
:dedent:
144+
:start-after: begin textIndex
145+
:end-before: end textIndex
146+
147+
.. _index-hashed-indexes:
148+
149+
Hashed Indexes
150+
--------------
151+
152+
A hashed index groups documents by the hash value in the indexed field.
153+
154+
In order to create a hashed index, first call the
155+
:java-core-api:`hashed() <com/mongodb/client/model/Indexes.html#hashed(java.lang.String)>`
156+
builder method to create a ``Bson`` instance that represents the index
157+
document, passing the name of the fields you want to index. Then, call
158+
the ``createIndex()`` method on the collection, passing the ``Bson``
159+
instance that contains the index document.
160+
161+
162+
The following example specifies a hashed index on the ``capacity``
163+
field:
164+
165+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
166+
:language: java
167+
:dedent:
168+
:start-after: begin hashedIndex
169+
:end-before: end hashedIndex
170+
171+
.. _index-geospatial-indexes:
172+
173+
Geospatial Indexes
174+
------------------
175+
176+
A geo2dsphere index groups documents by the coordinates in the indexed field.
177+
178+
In order to create a geo2dsphere index, first call the
179+
:java-core-api:`geo2dsphere() <com/mongodb/client/model/Indexes.html#geo2dsphere(java.lang.String...)>`
180+
builder method to create a ``Bson`` instance that represents the index
181+
document, passing the name or names of the fields you want to index.
182+
Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
183+
instance that contains the index document.
184+
185+
The following example specifies a geo2dsphere index on the ``location`` field:
186+
187+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
188+
:language: java
189+
:dedent:
190+
:start-after: begin geo2dsphereIndex
191+
:end-before: end geo2dsphereIndex
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package docs.builders;
2+
3+
import com.mongodb.client.MongoClient;
4+
import com.mongodb.client.MongoClients;
5+
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
7+
import com.mongodb.client.model.geojson.Point;
8+
import com.mongodb.client.model.geojson.Position;
9+
10+
import org.bson.Document;
11+
import org.bson.conversions.Bson;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import static com.mongodb.client.model.Indexes.*;
16+
17+
public class Indexes {
18+
19+
private final MongoClient mongoClient;
20+
private final MongoDatabase database;
21+
private MongoCollection<Document> collection;
22+
23+
private Indexes(){
24+
// begin declaration
25+
final String uri = System.getenv("DRIVER_REF_URI");
26+
27+
mongoClient = MongoClients.create(uri);
28+
database = mongoClient.getDatabase("builders");
29+
collection = database.getCollection("theatres");
30+
// end declaration
31+
}
32+
33+
public static void main(String[] args){
34+
Indexes indexes = new Indexes();
35+
// indexes.setupTheatreCollection();
36+
// indexes.preview();
37+
indexes.ascendingIndex();
38+
indexes.descendingIndex();
39+
indexes.compoundIndexExample();
40+
indexes.textIndex();
41+
indexes.hashedIndex();
42+
indexes.geo2dsphereIndex();
43+
}
44+
45+
private void ascendingIndex(){
46+
// begin ascendingIndex
47+
Bson ascendingIndex = ascending("name");
48+
collection.createIndex(ascendingIndex);
49+
// end ascendingIndex
50+
51+
// String resultCreateIndex = collection.createIndex(ascendingIndex);
52+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
53+
54+
}
55+
56+
private void descendingIndex(){
57+
// begin descendingIndex
58+
Bson descendingIndex = descending("capacity");
59+
collection.createIndex(descendingIndex);
60+
// end descendingIndex
61+
62+
// String resultCreateIndex = collection.createIndex(descendingIndex);
63+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
64+
}
65+
66+
private void compoundIndexExample(){
67+
// begin compoundIndexExample
68+
Bson compoundIndexExample = compoundIndex(descending("capacity", "year"), ascending("name"));
69+
collection.createIndex(compoundIndexExample);
70+
// end compoundIndexExample
71+
72+
// String resultCreateIndex = collection.createIndex(compoundIndexExample);
73+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
74+
}
75+
76+
private void textIndex(){
77+
// begin textIndex
78+
Bson textIndex = text("theaters");
79+
collection.createIndex(textIndex);
80+
// end textIndex
81+
82+
// String resultCreateIndex = collection.createIndex(textIndex);
83+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
84+
85+
}
86+
87+
private void hashedIndex(){
88+
// begin hashedIndex
89+
Bson hashedIndex = hashed("capacity");
90+
collection.createIndex(hashedIndex);
91+
// end hashedIndex
92+
93+
// String resultCreateIndex = collection.createIndex(hashedIndex);
94+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
95+
}
96+
97+
private void geo2dsphereIndex(){
98+
// begin geo2dsphereIndex
99+
Bson geo2dsphereIndex = geo2dsphere("location");
100+
collection.createIndex(geo2dsphereIndex);
101+
// end geo2dsphereIndex
102+
103+
// String resultCreateIndex = collection.createIndex(geo2dsphereIndex);
104+
// System.out.println(String.format("Index created: %s", resultCreateIndex));
105+
106+
}
107+
108+
private void preview(){
109+
Bson filter = new Document();
110+
List<Document> res = new ArrayList();
111+
System.out.println(collection.find(filter).into(res));
112+
// collection.drop();
113+
}
114+
115+
private void setupTheatreCollection() {
116+
List<Document> filterdata = new ArrayList<>();
117+
118+
Document p1 = new Document("_id", 1).append("name", "4K Theaters").append("logo", "red").append("capacity", 1076).append("year", 2021).append("location", new Point(new Position(-76.512016, 38.29697)));
119+
Document p2 = new Document("_id", 2).append("name", "Just Like Home").append("logo", "blue").append("capacity", 942).append("year", 1981).append("location", new Point(new Position(-121.96328, 38.367649)));
120+
Document p3 = new Document("_id", 3).append("name", "ABC Movies").append("logo", "blue").append("capacity", 1135).append("year", 2003).append("location", new Point(new Position(-86.642662, 33.605438)));
121+
Document p4 = new Document("_id", 4).append("name", "Classy Views").append("logo", "red").append("capacity", 847).append("year", 1998).append("location", new Point(new Position(-119.7412, 39.579536)));
122+
Document p5 = new Document("_id", 5).append("name", "Fortune Theaters").append("logo", "red").append("capacity", 1368).append("year", 2007).append("location", new Point(new Position(-100.81213, 46.829876)));
123+
Document p6 = new Document("_id", 6).append("name", "8K Screening").append("logo", "blue").append("capacity", 619).append("year", 2019).append("location", new Point(new Position(-118.11414, -118.114143)));
124+
Document p7 = new Document("_id", 7).append("name", "Date Night").append("logo", "red").append("capacity", 715).append("year", 2012).append("location", new Point(new Position(-100.50107, 31.435648)));
125+
Document p8 = new Document("_id", 8).append("name", "City's Best Theaters").append("logo", "red").append("capacity", 888).append("year", 2016).append("location", new Point(new Position(-78.382912, 40.490524)));
126+
Document p9 = new Document("_id", 9).append("name", "Pets Movies").append("logo", "blue").append("capacity", 1092).append("year", 2010).append("location", new Point(new Position(-117.674814, 33.590599)));
127+
Document p10 = new Document("_id", 10).append("name", "Fancy Theaters").append("logo", "red").append("capacity", 673).append("year", 2006).append("location", new Point(new Position(-72.583824, 41.998211)));
128+
129+
filterdata.add(p1);
130+
filterdata.add(p2);
131+
filterdata.add(p3);
132+
filterdata.add(p4);
133+
filterdata.add(p5);
134+
filterdata.add(p6);
135+
filterdata.add(p7);
136+
filterdata.add(p8);
137+
filterdata.add(p9);
138+
filterdata.add(p10);
139+
140+
collection.insertMany(filterdata);
141+
}
142+
143+
}

0 commit comments

Comments
 (0)