Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dbc0dd9

Browse files
committedAug 27, 2024
Address technical feedback
1 parent eda99fe commit dbc0dd9

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed
 

‎source/includes/usage-examples/index-code-examples.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.example;
2-
31
import com.mongodb.ConnectionString;
42
import com.mongodb.MongoClientSettings;
53
import com.mongodb.ServerApi;
@@ -11,9 +9,11 @@
119
import com.mongodb.client.model.Indexes;
1210
import com.mongodb.reactivestreams.client.*;
1311
import org.bson.Document;
12+
import org.reactivestreams.Publisher;
1413
import reactor.core.publisher.Flux;
14+
import reactor.core.publisher.Mono;
1515

16-
public class IndexOperations {
16+
public class IndexExamples {
1717
public static void main(String[] args) {
1818
// Replace the placeholder with your Atlas connection string
1919
String uri = "<connection string URI>";
@@ -33,21 +33,28 @@ public static void main(String[] args) {
3333
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
3434
MongoCollection<Document> collection = database.getCollection("movies");
3535

36+
Publisher<Void> dropAllResult = collection.dropIndexes();
37+
Mono.from(dropAllResult).block();
38+
3639
// start-single-field
37-
collection.createIndex(Indexes.ascending("<field name>"));
40+
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name>"));
41+
Mono.from(result).block();
3842
// end-single-field
3943

4044
// start-compound
41-
collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"));
45+
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"));
46+
Mono.from(result).block();
4247
// end-compound
4348

4449
// start-multikey
45-
collection.createIndex(Indexes.ascending("<array field name>"));
50+
Publisher<String> result = collection.createIndex(Indexes.ascending("<array field name>"));
51+
Mono.from(result).block();
4652
// end-multikey
4753

4854
// start-search-create
4955
Document index = new Document("mappings", new Document("dynamic", true));
50-
collection.createSearchIndex("<index name>", index);
56+
Publisher<String> result = collection.createSearchIndex("<index name>", index);
57+
Mono.from(result).block();
5158
// end-search-create
5259

5360
// start-search-list
@@ -60,28 +67,34 @@ public static void main(String[] args) {
6067

6168
// start-search-update
6269
Document newIndex = new Document("mappings", new Document("dynamic", true));
63-
collection.updateSearchIndex("<index name>", newIndex);
70+
Publisher<Void> result = collection.updateSearchIndex("<index name>", newIndex);
71+
Mono.from(result).block();
6472
// end-search-update
6573

6674
// start-search-delete
67-
collection.dropIndex("<index name>");
75+
Publisher<Void> result = collection.dropIndex("<index name>");
76+
Mono.from(result).block();
6877
// end-search-delete
6978

7079
// start-text
71-
collection.createIndex(Indexes.text("<field name>"));
80+
Publisher<String> result = collection.createIndex(Indexes.text("<field name>"));
81+
Mono.from(result).block();
7282
// end-text
7383

7484
// start-geo
75-
collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"));
85+
Publisher<String> result = collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"));
86+
Mono.from(result).block();
7687
// end-geo
7788

7889
// start-unique
7990
IndexOptions indexOptions = new IndexOptions().unique(true);
80-
collection.createIndex(Indexes.ascending("<field name>"), indexOptions);
91+
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name>"), indexOptions);
92+
Mono.from(result).block();
8193
// end-unique
8294

8395
// start-wildcard
84-
collection.createIndex(Indexes.ascending("$**"));
96+
Publisher<String> result = collection.createIndex(Indexes.ascending("$**"));
97+
Mono.from(result).block();
8598
// end-wildcard
8699

87100
// start-clustered
@@ -93,12 +106,14 @@ public static void main(String[] args) {
93106
CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions()
94107
.clusteredIndexOptions(clusteredIndexOptions);
95108

96-
MongoCollection<Document> collection = database.createCollection("<collection name>",
109+
Publisher<Void> clusteredCollection = database.createCollection("<collection name>",
97110
createCollectionOptions);
111+
Mono.from(clusteredCollection).block();
98112
// end-clustered
99113

100114
// start-remove
101-
collection.dropIndex("<index name>");
115+
Publisher<Void> result = collection.dropIndex("<index name>");
116+
Mono.from(result).block();
102117
// end-remove
103118
}
104119
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
You can use the following sample application to test the code examples on this
2+
page. To use the sample application, perform the following steps:
3+
4+
1. Create a new Java project in your IDE.
5+
#. Install the {+driver-short+} in your Java project.
6+
#. Install the `Project Reactor library
7+
<https://projectreactor.io/docs/core/release/reference/#getting>`__ in your
8+
Java project.
9+
#. Copy the following code and paste it into a new Java file named ``IndexApp.java``.
10+
#. Copy a code example from this page and paste it on the specified lines in the
11+
file.

‎source/includes/usage-examples/sample-index-application.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
package org.example;
2-
31
import com.mongodb.ConnectionString;
42
import com.mongodb.MongoClientSettings;
53
import com.mongodb.ServerApi;
64
import com.mongodb.ServerApiVersion;
75

6+
import com.mongodb.client.model.ClusteredIndexOptions;
7+
import com.mongodb.client.model.CreateCollectionOptions;
8+
import com.mongodb.client.model.IndexOptions;
89
import com.mongodb.client.model.Indexes;
910
import com.mongodb.reactivestreams.client.*;
1011
import org.bson.Document;
11-
import org.bson.conversions.Bson;
12+
import org.reactivestreams.Publisher;
1213
import reactor.core.publisher.Flux;
14+
import reactor.core.publisher.Mono;
15+
1316

1417
public class IndexOperations {
1518
public static void main(String[] args) {

0 commit comments

Comments
 (0)
Please sign in to comment.