Skip to content
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

DOCSP-39685: Indexes landing page #75

Merged
merged 9 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
105 changes: 105 additions & 0 deletions source/includes/usage-examples/index-code-examples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.example;

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 reactor.core.publisher.Flux;

public class IndexOperations {
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
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.

Similarly to other pages for the reactive driver, like #69, this one should both have the same note about Project Reactor Library, and actually execute the operations is demonstrates. collection.createIndex(Indexes.ascending("<field name>")) simply creates an object of the Publisher<String> type. It does not create an index. One way to execute the operation is, for example:

Mono<String> result = collection.createIndex(Indexes.ascending("<field name>"));
Mono.from(result).block();

This comment applies to many other examples in this PR.

// end-single-field

// start-compound
collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"));
// end-compound

// start-multikey
collection.createIndex(Indexes.ascending("<array field name>"));
// end-multikey

// start-search-create
Document index = new Document("mappings", new Document("dynamic", true));
collection.createSearchIndex("<index name>", index);
// 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));
collection.updateSearchIndex("<index name>", newIndex);
// end-search-update

// start-search-delete
collection.dropIndex("<index name>");
// end-search-delete

// start-text
collection.createIndex(Indexes.text("<field name>"));
// end-text

// start-geo
collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"));
// end-geo

// start-unique
IndexOptions indexOptions = new IndexOptions().unique(true);
collection.createIndex(Indexes.ascending("<field name>"), indexOptions);
// end-unique

// start-wildcard
collection.createIndex(Indexes.ascending("$**"));
// end-wildcard

// start-clustered
ClusteredIndexOptions clusteredIndexOptions = new ClusteredIndexOptions(
Indexes.ascending("_id"),
true
);

CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions()
.clusteredIndexOptions(clusteredIndexOptions);

MongoCollection<Document> collection = database.createCollection("<collection name>",
Copy link
Member

Choose a reason for hiding this comment

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

The collection variable is declared in sample-index-application.java on line 32, which means, the another collection variable cannot be declared here.

Suggested change
MongoCollection<Document> collection = database.createCollection("<collection name>",
MongoCollection<Document> clusteredCollection = database.createCollection("<collection name>",

Copy link
Collaborator Author

@mcmorisi mcmorisi Aug 27, 2024

Choose a reason for hiding this comment

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

I can make this change for clarity's sake.

With that said, the individual examples in this code are intended to be separately copy-pasted by readers into the sample application provided at the start of the page (as opposed to running the entire sample-index-application.java file on its own). There may be some variable reuse in implementing your other feedback, FYI.

Copy link
Member

Choose a reason for hiding this comment

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

It's not for clarity. If you actually try to copy this example into the sample application, you'll discover that it does not compile for the reason I specified.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, I get it now! Appreciate the callouts!!

Copy link
Member

Choose a reason for hiding this comment

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

database.createCollection does not return MongoCollection<Document>, it returns Publisher<Void>. Could you please try each example, to make sure it compiles and runs successfully?

createCollectionOptions);
// end-clustered

// start-remove
collection.dropIndex("<index name>");
// end-remove
}
}
}
39 changes: 39 additions & 0 deletions source/includes/usage-examples/sample-index-application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.example;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;

import com.mongodb.client.model.Indexes;
import com.mongodb.reactivestreams.client.*;
import org.bson.Document;
import org.bson.conversions.Bson;
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.


public class IndexOperations {
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