-
Notifications
You must be signed in to change notification settings - Fork 17
DOCSP-39685: Indexes landing page #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
c8c26ed
7c6ad5a
eda99fe
dbc0dd9
6699013
f89382b
90d0245
638a476
4a07841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import com.mongodb.ConnectionString; | ||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.ServerApi; | ||
import com.mongodb.ServerApiVersion; | ||
|
||
import com.mongodb.client.model.ClusteredIndexOptions; | ||
import com.mongodb.client.model.CreateCollectionOptions; | ||
import com.mongodb.client.model.IndexOptions; | ||
import com.mongodb.client.model.Indexes; | ||
import com.mongodb.reactivestreams.client.*; | ||
import org.bson.Document; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class IndexExamples { | ||
public static void main(String[] args) { | ||
// Replace the placeholder with your Atlas connection string | ||
String uri = "<connection string URI>"; | ||
|
||
// Construct a ServerApi instance using the ServerApi.builder() method | ||
ServerApi serverApi = ServerApi.builder() | ||
.version(ServerApiVersion.V1) | ||
.build(); | ||
|
||
MongoClientSettings settings = MongoClientSettings.builder() | ||
.applyConnectionString(new ConnectionString(uri)) | ||
.serverApi(serverApi) | ||
.build(); | ||
|
||
// Create a new client and connect to the server | ||
try (MongoClient mongoClient = MongoClients.create(settings)) { | ||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||
MongoCollection<Document> collection = database.getCollection("movies"); | ||
|
||
// start-single-field | ||
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name>")); | ||
Mono.from(result).block(); | ||
// end-single-field | ||
|
||
// start-compound | ||
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>")); | ||
Mono.from(result).block(); | ||
// end-compound | ||
|
||
// start-multikey | ||
Publisher<String> result = collection.createIndex(Indexes.ascending("<array field name>")); | ||
Mono.from(result).block(); | ||
// end-multikey | ||
|
||
// start-search-create | ||
Document index = new Document("mappings", new Document("dynamic", true)); | ||
Publisher<String> result = collection.createSearchIndex("<index name>", index); | ||
Mono.from(result).block(); | ||
// end-search-create | ||
|
||
// start-search-list | ||
ListSearchIndexesPublisher<Document> listIndexesPublisher = collection.listSearchIndexes(); | ||
|
||
Flux.from(listIndexesPublisher) | ||
.doOnNext(System.out::println) | ||
.blockLast(); | ||
// end-search-list | ||
|
||
// start-search-update | ||
Document newIndex = new Document("mappings", new Document("dynamic", true)); | ||
Publisher<Void> result = collection.updateSearchIndex("<index name>", newIndex); | ||
Mono.from(result).block(); | ||
// end-search-update | ||
|
||
// start-search-delete | ||
Publisher<Void> result = collection.dropIndex("<index name>"); | ||
Mono.from(result).block(); | ||
// end-search-delete | ||
|
||
// start-text | ||
Publisher<String> result = collection.createIndex(Indexes.text("<field name>")); | ||
Mono.from(result).block(); | ||
// end-text | ||
|
||
// start-geo | ||
Publisher<String> result = collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>")); | ||
Mono.from(result).block(); | ||
// end-geo | ||
|
||
// start-unique | ||
IndexOptions indexOptions = new IndexOptions().unique(true); | ||
Publisher<String> result = collection.createIndex(Indexes.ascending("<field name>"), indexOptions); | ||
Mono.from(result).block(); | ||
// end-unique | ||
|
||
// start-wildcard | ||
Publisher<String> result = collection.createIndex(Indexes.ascending("$**")); | ||
Mono.from(result).block(); | ||
// end-wildcard | ||
|
||
// start-clustered | ||
ClusteredIndexOptions clusteredIndexOptions = new ClusteredIndexOptions( | ||
Indexes.ascending("_id"), | ||
true | ||
); | ||
|
||
CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions() | ||
.clusteredIndexOptions(clusteredIndexOptions); | ||
|
||
Publisher<Void> clusteredCollection = database.createCollection("<collection name>", | ||
createCollectionOptions); | ||
Mono.from(clusteredCollection).block(); | ||
// end-clustered | ||
|
||
// start-remove | ||
Publisher<Void> result = collection.dropIndex("<index name>"); | ||
Mono.from(result).block(); | ||
// end-remove | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
You can use the following sample application to test the code examples on this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems out of place, given other examples in the documentation don't provide such information. Generally, they seem to provide a block about the examples using Project reactor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add the extra info about using Project reactor to be more in line with the Read/Write landing pages. |
||
page. To use the sample application, perform the following steps: | ||
|
||
1. Create a new Java project in your IDE. | ||
#. Install the {+driver-short+} in your Java project. | ||
#. Install the `Project Reactor library | ||
<https://projectreactor.io/docs/core/release/reference/#getting>`__ in your | ||
Java project. | ||
#. Copy the following code and paste it into a new Java file named ``IndexApp.java``. | ||
#. Copy a code example from this page and paste it on the specified lines in the | ||
file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import com.mongodb.ConnectionString; | ||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.ServerApi; | ||
import com.mongodb.ServerApiVersion; | ||
|
||
import com.mongodb.client.model.ClusteredIndexOptions; | ||
import com.mongodb.client.model.CreateCollectionOptions; | ||
import com.mongodb.client.model.IndexOptions; | ||
import com.mongodb.client.model.Indexes; | ||
import com.mongodb.reactivestreams.client.*; | ||
import org.bson.Document; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The imports here must be the same as in |
||
import reactor.core.publisher.Mono; | ||
|
||
public class IndexApp { | ||
public static void main(String[] args) { | ||
// Replace the placeholder with your Atlas connection string | ||
String uri = "<connection string URI>"; | ||
|
||
// Construct a ServerApi instance using the ServerApi.builder() method | ||
ServerApi serverApi = ServerApi.builder() | ||
.version(ServerApiVersion.V1) | ||
.build(); | ||
|
||
MongoClientSettings settings = MongoClientSettings.builder() | ||
.applyConnectionString(new ConnectionString(uri)) | ||
.serverApi(serverApi) | ||
.build(); | ||
|
||
// Create a new client and connect to the server | ||
try (MongoClient mongoClient = MongoClients.create(settings)) { | ||
MongoDatabase database = mongoClient.getDatabase("<database name>"); | ||
MongoCollection<Document> collection = database.getCollection("<collection name>"); | ||
|
||
// Start example code here | ||
|
||
// End example code here | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
result
is a misleading variable name as nothing has happened, these methods return aPublisher
and nothing happens until its subscribed to.Recommend renaming all variables called result.