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+ }
0 commit comments