1
- package org .example ;
2
-
3
1
import com .mongodb .ConnectionString ;
4
2
import com .mongodb .MongoClientSettings ;
5
3
import com .mongodb .ServerApi ;
11
9
import com .mongodb .client .model .Indexes ;
12
10
import com .mongodb .reactivestreams .client .*;
13
11
import org .bson .Document ;
12
+ import org .reactivestreams .Publisher ;
14
13
import reactor .core .publisher .Flux ;
14
+ import reactor .core .publisher .Mono ;
15
15
16
- public class IndexOperations {
16
+ public class IndexExamples {
17
17
public static void main (String [] args ) {
18
18
// Replace the placeholder with your Atlas connection string
19
19
String uri = "<connection string URI>" ;
@@ -33,21 +33,28 @@ public static void main(String[] args) {
33
33
MongoDatabase database = mongoClient .getDatabase ("sample_mflix" );
34
34
MongoCollection <Document > collection = database .getCollection ("movies" );
35
35
36
+ Publisher <Void > dropAllResult = collection .dropIndexes ();
37
+ Mono .from (dropAllResult ).block ();
38
+
36
39
// 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 ();
38
42
// end-single-field
39
43
40
44
// 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 ();
42
47
// end-compound
43
48
44
49
// 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 ();
46
52
// end-multikey
47
53
48
54
// start-search-create
49
55
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 ();
51
58
// end-search-create
52
59
53
60
// start-search-list
@@ -60,28 +67,34 @@ public static void main(String[] args) {
60
67
61
68
// start-search-update
62
69
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 ();
64
72
// end-search-update
65
73
66
74
// start-search-delete
67
- collection .dropIndex ("<index name>" );
75
+ Publisher <Void > result = collection .dropIndex ("<index name>" );
76
+ Mono .from (result ).block ();
68
77
// end-search-delete
69
78
70
79
// start-text
71
- collection .createIndex (Indexes .text ("<field name>" ));
80
+ Publisher <String > result = collection .createIndex (Indexes .text ("<field name>" ));
81
+ Mono .from (result ).block ();
72
82
// end-text
73
83
74
84
// 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 ();
76
87
// end-geo
77
88
78
89
// start-unique
79
90
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 ();
81
93
// end-unique
82
94
83
95
// start-wildcard
84
- collection .createIndex (Indexes .ascending ("$**" ));
96
+ Publisher <String > result = collection .createIndex (Indexes .ascending ("$**" ));
97
+ Mono .from (result ).block ();
85
98
// end-wildcard
86
99
87
100
// start-clustered
@@ -93,12 +106,14 @@ public static void main(String[] args) {
93
106
CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions ()
94
107
.clusteredIndexOptions (clusteredIndexOptions );
95
108
96
- MongoCollection < Document > collection = database .createCollection ("<collection name>" ,
109
+ Publisher < Void > clusteredCollection = database .createCollection ("<collection name>" ,
97
110
createCollectionOptions );
111
+ Mono .from (clusteredCollection ).block ();
98
112
// end-clustered
99
113
100
114
// start-remove
101
- collection .dropIndex ("<index name>" );
115
+ Publisher <Void > result = collection .dropIndex ("<index name>" );
116
+ Mono .from (result ).block ();
102
117
// end-remove
103
118
}
104
119
}
0 commit comments