-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8c26ed
DOCSP-39685: Indexes landing page
mcmorisi 7c6ad5a
Fix
mcmorisi eda99fe
Address NR feedback
mcmorisi dbc0dd9
Address technical feedback
mcmorisi 6699013
Fix
mcmorisi f89382b
fix
mcmorisi 90d0245
Fix
mcmorisi 638a476
Fix
mcmorisi 4a07841
Address further technical feedback
mcmorisi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
source/includes/usage-examples/index-code-examples.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>")); | ||
// 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", | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createCollectionOptions); | ||
// end-clustered | ||
|
||
// start-remove | ||
collection.dropIndex("<index name>"); | ||
// end-remove | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
source/includes/usage-examples/sample-index-application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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 |
||
|
||
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 thePublisher<String>
type. It does not create an index. One way to execute the operation is, for example:This comment applies to many other examples in this PR.