Skip to content

Commit 8b3d199

Browse files
authored
DOCSP-39685: Indexes landing page (#75)
1 parent f9e7151 commit 8b3d199

File tree

4 files changed

+367
-169
lines changed

4 files changed

+367
-169
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import com.mongodb.ConnectionString;
2+
import com.mongodb.MongoClientSettings;
3+
import com.mongodb.ServerApi;
4+
import com.mongodb.ServerApiVersion;
5+
6+
import com.mongodb.client.model.ClusteredIndexOptions;
7+
import com.mongodb.client.model.CreateCollectionOptions;
8+
import com.mongodb.client.model.IndexOptions;
9+
import com.mongodb.client.model.Indexes;
10+
import com.mongodb.reactivestreams.client.*;
11+
import org.bson.Document;
12+
import org.reactivestreams.Publisher;
13+
import reactor.core.publisher.Flux;
14+
import reactor.core.publisher.Mono;
15+
16+
public class IndexExamples {
17+
public static void main(String[] args) {
18+
// Replace the placeholder with your Atlas connection string
19+
String uri = "<connection string URI>";
20+
21+
// Construct a ServerApi instance using the ServerApi.builder() method
22+
ServerApi serverApi = ServerApi.builder()
23+
.version(ServerApiVersion.V1)
24+
.build();
25+
26+
MongoClientSettings settings = MongoClientSettings.builder()
27+
.applyConnectionString(new ConnectionString(uri))
28+
.serverApi(serverApi)
29+
.build();
30+
31+
// Create a new client and connect to the server
32+
try (MongoClient mongoClient = MongoClients.create(settings)) {
33+
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
34+
MongoCollection<Document> collection = database.getCollection("movies");
35+
36+
// start-single-field
37+
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>"));
38+
Mono.from(publisher).block();
39+
// end-single-field
40+
41+
// start-compound
42+
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"));
43+
Mono.from(publisher).block();
44+
// end-compound
45+
46+
// start-multikey
47+
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<array field name>"));
48+
Mono.from(publisher).block();
49+
// end-multikey
50+
51+
// start-search-create
52+
Document index = new Document("mappings", new Document("dynamic", true));
53+
Publisher<String> publisher = collection.createSearchIndex("<index name>", index);
54+
Mono.from(publisher).block();
55+
// end-search-create
56+
57+
// start-search-list
58+
ListSearchIndexesPublisher<Document> listIndexesPublisher = collection.listSearchIndexes();
59+
60+
Flux.from(listIndexesPublisher)
61+
.doOnNext(System.out::println)
62+
.blockLast();
63+
// end-search-list
64+
65+
// start-search-update
66+
Document newIndex = new Document("mappings", new Document("dynamic", true));
67+
Publisher<Void> publisher = collection.updateSearchIndex("<index name>", newIndex);
68+
Mono.from(publisher).block();
69+
// end-search-update
70+
71+
// start-search-delete
72+
Publisher<Void> publisher = collection.dropIndex("<index name>");
73+
Mono.from(publisher).block();
74+
// end-search-delete
75+
76+
// start-text
77+
Publisher<String> publisher = collection.createIndex(Indexes.text("<field name>"));
78+
Mono.from(publisher).block();
79+
// end-text
80+
81+
// start-geo
82+
Publisher<String> publisher = collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"));
83+
Mono.from(publisher).block();
84+
// end-geo
85+
86+
// start-unique
87+
IndexOptions indexOptions = new IndexOptions().unique(true);
88+
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>"), indexOptions);
89+
Mono.from(publisher).block();
90+
// end-unique
91+
92+
// start-wildcard
93+
Publisher<String> publisher = collection.createIndex(Indexes.ascending("$**"));
94+
Mono.from(publisher).block();
95+
// end-wildcard
96+
97+
// start-clustered
98+
ClusteredIndexOptions clusteredIndexOptions = new ClusteredIndexOptions(
99+
Indexes.ascending("_id"),
100+
true
101+
);
102+
103+
CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions()
104+
.clusteredIndexOptions(clusteredIndexOptions);
105+
106+
Publisher<Void> clusteredCollection = database.createCollection("<collection name>",
107+
createCollectionOptions);
108+
Mono.from(clusteredCollection).block();
109+
// end-clustered
110+
111+
// start-remove
112+
Publisher<Void> publisher = collection.dropIndex("<index name>");
113+
Mono.from(publisher).block();
114+
// end-remove
115+
}
116+
}
117+
}
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.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import com.mongodb.ConnectionString;
2+
import com.mongodb.MongoClientSettings;
3+
import com.mongodb.ServerApi;
4+
import com.mongodb.ServerApiVersion;
5+
6+
import com.mongodb.client.model.ClusteredIndexOptions;
7+
import com.mongodb.client.model.CreateCollectionOptions;
8+
import com.mongodb.client.model.IndexOptions;
9+
import com.mongodb.client.model.Indexes;
10+
import com.mongodb.reactivestreams.client.*;
11+
import org.bson.Document;
12+
import org.reactivestreams.Publisher;
13+
import reactor.core.publisher.Flux;
14+
import reactor.core.publisher.Mono;
15+
16+
public class IndexApp {
17+
public static void main(String[] args) {
18+
// Replace the placeholder with your Atlas connection string
19+
String uri = "<connection string URI>";
20+
21+
// Construct a ServerApi instance using the ServerApi.builder() method
22+
ServerApi serverApi = ServerApi.builder()
23+
.version(ServerApiVersion.V1)
24+
.build();
25+
26+
MongoClientSettings settings = MongoClientSettings.builder()
27+
.applyConnectionString(new ConnectionString(uri))
28+
.serverApi(serverApi)
29+
.build();
30+
31+
// Create a new client and connect to the server
32+
try (MongoClient mongoClient = MongoClients.create(settings)) {
33+
MongoDatabase database = mongoClient.getDatabase("<database name>");
34+
MongoCollection<Document> collection = database.getCollection("<collection name>");
35+
36+
// Start example code here
37+
38+
// End example code here
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)