@@ -37,6 +37,7 @@ private IndexPage() {
37
37
38
38
public static void main (String [] args ) {
39
39
IndexPage page = new IndexPage ();
40
+
40
41
page .singleIndex ();
41
42
page .compoundIndex ();
42
43
page .wildCardIndex ();
@@ -48,109 +49,136 @@ public static void main(String[] args) {
48
49
49
50
private void singleIndex () {
50
51
System .out .println ("single index" );
52
+
53
+ // Creates an index on the "title" field in ascending order
51
54
// begin single index
52
55
String resultCreateIndex = collection .createIndex (Indexes .ascending ("title" ));
53
56
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
54
57
// end single index
55
58
59
+ // Retrieves matching documents directly from the "title" index, applying a sort and projection
56
60
// begin covered single query
57
61
Bson filter = eq ("title" , "Batman" );
58
62
Bson sort = Sorts .ascending ("title" );
59
63
Bson projection = fields (include ("title" ), excludeId ());
60
64
FindIterable <Document > cursor = collection .find (filter ).sort (sort ).projection (projection );
61
65
// end covered single query
66
+
67
+ // Prints the results of the find operation
62
68
cursor .forEach (doc -> System .out .println (doc ));
63
69
64
70
}
65
71
66
72
private void compoundIndex () {
67
73
System .out .println ("compound index" );
74
+
75
+ // Creates a compound index on the "type" and "rated" fields in ascending order
68
76
// begin compound index
69
77
String resultCreateIndex = collection .createIndex (Indexes .ascending ("type" , "rated" ));
70
78
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
71
79
// end compound index
72
80
81
+ // Retrieves matching documents directly from the compound index, applying a sort and projection
73
82
// begin covered compound query
74
83
Bson filter = and (eq ("type" , "movie" ), eq ("rated" , "G" ));
75
84
Bson sort = Sorts .ascending ("type" , "rated" );
76
85
Bson projection = fields (include ("type" , "rated" ), excludeId ());
77
86
FindIterable <Document > cursor = collection .find (filter ).sort (sort ).projection (projection );
78
87
// end covered compound query
88
+
89
+ // Prints the results of the find operation
79
90
cursor .forEach (doc -> System .out .println (doc ));
80
91
}
81
92
82
93
private void multiKeyIndex () {
83
94
System .out .println ("multikey index" );
95
+ // Creates a compound multikey index on the "rated", "genres", and "title" fields in ascending order
84
96
// begin multikey index
85
97
String resultCreateIndex = collection .createIndex (Indexes .ascending ("rated" , "genres" , "title" ));
86
98
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
87
99
// end multikey index
88
100
101
+ // Retrieves matching documents directly from the multikey index, applying a sort and projection
89
102
// begin covered multikey query
90
103
Bson filter = and (eq ("genres" , "Animation" ), eq ("rated" , "G" ));
91
104
Bson sort = Sorts .ascending ("title" );
92
105
Bson projection = fields (include ("title" , "rated" ), excludeId ());
93
106
FindIterable <Document > cursor = collection .find (filter ).sort (sort ).projection (projection );
94
107
// end covered multikey query
108
+
109
+ // Prints the results of the find operation
95
110
cursor .forEach (doc -> System .out .println (doc ));
96
111
}
97
112
98
113
private void textIndex () {
99
114
System .out .println ("text index" );
115
+ // Creates a text index on the "plot" field
100
116
// begin text index
101
- // create a text index of the "plot" field in the "movies" collection
102
- // if a text index already exists with a different configuration, this will
103
- // error
104
117
try {
105
118
String resultCreateIndex = collection .createIndex (Indexes .text ("plot" ));
106
119
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
120
+
121
+ // Prints a message if a text index already exists with a different configuration
107
122
} catch (MongoCommandException e ) {
108
123
if (e .getErrorCodeName ().equals ("IndexOptionsConflict" ))
109
124
System .out .println ("there's an existing text index with different options" );
110
125
}
111
126
// end text index
112
127
128
+ // Retrieves matching documents directly from the text index, applying a projection
113
129
// begin text query
114
130
Bson filter = text ("java coffee shop" );
115
131
Bson projection = fields (include ("fullplot" ), excludeId ());
116
132
FindIterable <Document > cursor = collection .find (filter ).projection (projection );
117
133
// end text query
134
+
135
+ // Prints the results of the find operation
118
136
cursor .forEach (doc -> System .out .println (doc ));
119
137
}
120
138
121
139
private void geoSpatialIndex () {
122
140
System .out .println ("geospatial index" );
123
141
collection = database .getCollection ("theaters" );
142
+
143
+ // Creates a geospatial index on the "location.geo" field
124
144
// begin geospatial index
125
- // if an existing geo index exists, this will error
126
145
try {
127
146
String resultCreateIndex = collection .createIndex (Indexes .geo2dsphere ("location.geo" ));
128
147
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
148
+
149
+ // Prints a message if a geospatial index already exists with a different configuration
129
150
} catch (MongoCommandException e ) {
130
151
if (e .getErrorCodeName ().equals ("IndexOptionsConflict" ))
131
- System .out .println ("there's an existing text index with different options" );
152
+ System .out .println ("there's an existing geospatial index with different options" );
132
153
}
133
154
// end geospatial index
134
155
135
156
// begin geospatial query
136
- // MongoDB Headquarters in NY, NY.
157
+ // Stores the coordinates of the NY MongoDB headquarters
137
158
Point refPoint = new Point (new Position (-73.98456 , 40.7612 ));
159
+
160
+ // Retrieves documents that represent locations up to 1000 meters from the specified point directly from the geospatial index
161
+ // Creates a filter to match a document
138
162
Bson filter = near ("location.geo" , refPoint , 1000.0 , 0.0 );
139
163
FindIterable <Document > cursor = collection .find (filter );
140
164
// end geospatial query
165
+
166
+ // Prints the results of the find operation
141
167
cursor .forEach (doc -> System .out .println (doc ));
142
168
}
143
169
144
170
private void uniqueIndex () {
145
171
System .out .println ("unique index" );
146
172
collection = database .getCollection ("theaters" );
147
173
174
+ // Creates a unique index on the "theaterID" field in descending order
148
175
// begin unique index
149
- // this will fail if any duplicate values exist on the field you are indexing
150
176
try {
151
177
IndexOptions indexOptions = new IndexOptions ().unique (true );
152
178
String resultCreateIndex = collection .createIndex (Indexes .descending ("theaterId" ), indexOptions );
153
179
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
180
+
181
+ // Prints a message if the "theaterID" field contains duplicate values
154
182
} catch (DuplicateKeyException e ) {
155
183
System .out .printf ("duplicate field values encountered, couldn't create index: \t %s\n " , e );
156
184
}
@@ -160,6 +188,7 @@ private void wildcardIndex() {
160
188
System .out .println ("wildcard index" );
161
189
collection = database .getCollection ("theaters" );
162
190
191
+ // Creates a wildcard index on all values of the "location" field in ascending order
163
192
// begin wildcard index
164
193
String resultCreateIndex = collection .createIndex (Indexes .ascending ("location.$**" ));
165
194
System .out .println (String .format ("Index created: %s" , resultCreateIndex ));
0 commit comments