Skip to content

Commit f1565d4

Browse files
committed
DOCSP-33345: Java comments pt. 7 (#476)
* DOCSP-33345: Java comments pt. 7 * more comments * reword (cherry picked from commit 4831458)
1 parent 14aff9e commit f1565d4

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

source/includes/fundamentals/code-snippets/JsonFormats.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,33 @@ public static void main(String[] args) {
2929

3030
try {
3131
ObjectId testId = new ObjectId("507f1f77bcf86cd799439013");
32+
33+
// Creates a document that has a specified "_id" value
3234
Document idDocument = new Document().append("_id", testId);
3335

36+
// Deletes a document that has a specified "_id" value and inserts a document with the same "_id" value
3437
collection.deleteOne(idDocument);
3538
collection.insertOne(new Document()
3639
.append("_id", testId)
3740
.append("createdAt", new Date())
3841
.append("myNumber", 36520312L));
42+
43+
// Retrieves the first document that has the specified "_id" value
3944
Document doc = collection.find(idDocument)
4045
.first();
4146

47+
// Prints the result of the find operation in multiple JSON formats
4248
for (JsonMode mode : modes) {
4349
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(mode).build();
4450
System.out.println(mode.name() + ":" + doc.toJson(settings));
4551
}
52+
53+
// Prints exception details if any exceptions occur during the find operation
4654
} catch (MongoException me) {
4755
System.err.println(me);
4856
}
57+
58+
// Prints exception details if any exceptions occur during the client and dataset setup
4959
} catch (Exception e) {
5060
System.err.println(e);
5161
}

source/includes/fundamentals/code-snippets/LegacyAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ public static void main(String[] args) {
1515
}
1616

1717
private void example1() {
18+
// Connects to a MongoDB instance with the legacy API
1819
// start legacy-api-example
1920
MongoClient client = new MongoClient(URI);
2021
DB db = client.getDB(DATABASE);
2122
DBCollection col = db.getCollection(COLLECTION);
23+
24+
// Retrieves one document in the collection and prints it
2225
DBObject doc = col.find().one();
2326
System.out.println(doc.toString());
2427
// end legacy-api-example
2528
client.close();
2629
}
2730

2831
private void example2() {
32+
// Creates a MongoClient with the legacy API that requests write acknowledgement from at least one node
2933
// start legacy-api-mongoclientoptions-example
3034
MongoClientURI mongoURI = new MongoClientURI(URI,
3135
MongoClientOptions.builder()

source/includes/fundamentals/code-snippets/MongoDbAwsAuth.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,38 @@ public class MongoDbAwsAuth {
1818
// Placeholder functions
1919

2020
private static void encodeText() throws UnsupportedEncodingException {
21+
// Defines the encoding scheme used to translate a string
2122
// start urlEncode
2223
String encodedField = java.net.URLEncoder.encode("<fieldValue>".toString(), "ISO-8859-1");
2324
// end urlEncode
2425
}
2526
private static void connectionString() {
27+
// Creates a MongoClient and provides AWS credentials to enable the MONGODB-AWS authentication mechanism
2628
// start connectionString
2729
MongoClient mongoClient = MongoClients.create("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS");
2830
// end connectionString
2931
}
3032

3133
private static void mechOnlyConnectionString() {
34+
// Creates a MongoClient that MongoDB authenticates by using the MONGODB-AWS mechanism
3235
// start mechOnlyConnectionString
3336
MongoClient mongoClient = MongoClients.create("mongodb://<atlasUri>?authMechanism=MONGODB-AWS");
3437
// end mechOnlyConnectionString
3538
}
3639

3740
private static void connectionStringSessionToken() {
41+
// Creates a MongoClient and provides AWS credentials and a session token to enable the MONGODB-AWS authentication mechanism
3842
// start connectionStringSessionToken
3943
MongoClient mongoClient = MongoClients.create("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
4044
// end connectionStringSessionToken
4145
}
4246

4347
private static void mongoCredential() {
48+
// Creates a MongoCredential instance to specify the AWS credentials
4449
// start mongoCredential
4550
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray());
4651

52+
// Creates a MongoClient that receives AWS credentials from the MongoCredential instance
4753
MongoClient mongoClient = MongoClients.create(
4854
MongoClientSettings.builder()
4955
.applyToClusterSettings(builder ->
@@ -54,9 +60,11 @@ private static void mongoCredential() {
5460
}
5561

5662
private static void mechOnlyMongoCredential() {
63+
// Creates a MongoCredential instance to specify the authentication mechanism
5764
// start mechOnlyMongoCredential
5865
MongoCredential credential = MongoCredential.createAwsCredential(null, null);
5966

67+
// Creates a MongoClient that receives configuration information from a MongoCredential and environment variables
6068
MongoClient mongoClient = MongoClients.create(
6169
MongoClientSettings.builder()
6270
.applyToClusterSettings(builder ->
@@ -67,10 +75,14 @@ private static void mechOnlyMongoCredential() {
6775
}
6876

6977
private static void mongoCredentialSessionTokenConnString() {
78+
// Creates a MongoCredential instance to specify the AWS credentials
7079
// start mongoCredentialSessionTokenConnString
7180
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray());
81+
82+
// Specifies the authentication mechanism and session token in a connection string
7283
ConnectionString connectionString = new ConnectionString("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
7384

85+
// Creates a MongoClient that receives configuration information from a MongoCrediential and connection string
7486
MongoClient mongoClient = MongoClients.create(
7587
MongoClientSettings.builder()
7688
.applyConnectionString(connectionString)
@@ -80,9 +92,11 @@ private static void mongoCredentialSessionTokenConnString() {
8092
}
8193

8294
private static void mongoCredentialSessionTokenCredential() {
95+
// Creates a MongoCredential instance to specify the AWS credentials and a session token
8396
// start mongoCredentialSessionTokenCredential
8497
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray()).withMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>");
8598

99+
// Creates a MongoClient that receives configuration information from a MongoCredential instance
86100
MongoClient mongoClient = MongoClients.create(
87101
MongoClientSettings.builder()
88102
.applyToClusterSettings(builder ->
@@ -93,23 +107,26 @@ private static void mongoCredentialSessionTokenCredential() {
93107
}
94108

95109
private static void mongoCredentialECSorEC2() {
110+
// Creates a MongoCredential instance to specify the authentication mechanism
96111
// start mongoCredentialECSorEC2
97112
MongoCredential credential = MongoCredential.createAwsCredential(null, null);
98113
// end mongoCredentialECSorEC2
99114
}
100115

101116
private static void refreshCredentials() {
102-
117+
// Creates a lambda expression that returns new AWS credentials
103118
// start refreshCredentials
104119
Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
105-
// Add your code here to fetch new credentials here
120+
// Add your code to fetch new credentials
106121

107-
// Return the new credentials
108122
return new AwsCredential("<awsKeyId>", "<awsSecretKey>", "<awsSessionToken>");
109123
};
110124

125+
// Creates a MongoCredential instance to specify the new AWS credentials
111126
MongoCredential credential = MongoCredential.createAwsCredential(null, null)
112127
.withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
128+
129+
// Creates a MongoClient that receives new configuration information from a MongoCredential instance
113130
MongoClient mongoClient = MongoClients.create(
114131
MongoClientSettings.builder()
115132
.applyToClusterSettings(builder ->

0 commit comments

Comments
 (0)