-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 3 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,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>", | ||||||
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
Suggested change
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 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 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. 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. 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. Ah, I get it now! Appreciate the callouts!! 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.
|
||||||
createCollectionOptions); | ||||||
// end-clustered | ||||||
|
||||||
// start-remove | ||||||
collection.dropIndex("<index name>"); | ||||||
// end-remove | ||||||
} | ||||||
} | ||||||
} |
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 | ||
} | ||
} | ||
} |
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.