Skip to content

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

Merged
merged 9 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions source/includes/usage-examples/index-code-examples.java
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>"));
Copy link
Member

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 a Publisher and nothing happens until its subscribed to.

Recommend renaming all variables called result.

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
}
}
}
11 changes: 11 additions & 0 deletions source/includes/usage-examples/sample-index-app-intro.rst
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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
41 changes: 41 additions & 0 deletions source/includes/usage-examples/sample-index-application.java
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports here must be the same as in index-code-examples.java, otherwise some code from index-code-examples.java may not compile when copied in IndexOperations.

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
}
}
}
Loading
Loading