Connecting Programmatically to Amazon DocumentDB
This section contains code examples that demonstrate how to connect to Amazon DocumentDB (with MongoDB compatibility) using several different languages. The examples are separated into two sections based on whether you are connecting to a cluster that has Transport Layer Security (TLS) enabled or disabled. By default, TLS is enabled on Amazon DocumentDB clusters. However, you can turn off TLS if you want. For more information, see Encrypting Data in Transit.
If you are attempting to connect to your Amazon DocumentDB from outside the VPC in which your cluster resides, please see Connecting to an Amazon DocumentDB Cluster from Outside an Amazon VPC.
Before you connect to your cluster, you must know whether TLS is enabled on the cluster. The next section shows you how to determine the value of your cluster’s tls
parameter using either the AWS Management Console or the AWS CLI. Following that, you can continue by finding and applying the appropriate code example.
Determining the Value of Your tls
Parameter
Determining whether your cluster has TLS enabled is a two-step process that you can perform using either the AWS Management Console or AWS CLI.
Determine which parameter group is governing your cluster.
Sign in to the AWS Management Console, and open the Amazon DocumentDB console at https://console.aws.amazon.com/docdb.
In the left navigation pane, choose Clusters.
In the list of clusters, select the name of your cluster.
The resulting page shows the details of the cluster that you selected. Scroll down to Cluster details. At the bottom of that section, locate the parameter group’s name below Cluster parameter group.
The following AWS CLI code determines which parameter is governing your cluster. Make sure you replace
sample-cluster
with the name of your cluster.aws docdb describe-db-clusters \
--db-cluster-identifier sample-cluster \
--query 'DBClusters[*].[DBClusterIdentifier,DBClusterParameterGroup]'
Output from this operation looks something like the following:
[
[
"sample-cluster",
"sample-parameter-group"
]
]
Determine the value of the
tls
parameter in your cluster’s parameter group.In the navigation pane, choose Parameter groups.
In the Cluster parameter groups window, select your cluster parameter group.
The resulting page shows your cluster parameter group’s parameters. You can see the value of the
tls
parameter here. For information on modifying this parameter, see Modifying Amazon DocumentDB Cluster Parameter Groups.
You can use the
describe-db-cluster-parameters
AWS CLI command to view the details of the parameters in your cluster parameter group.--describe-db-cluster-parameters
— To list all the parameters inside a parameter group and their values.--db-cluster-parameter-group name
— Required. The name of your cluster parameter group.
```
aws docdb describe-db-cluster-parameters \
--db-cluster-parameter-group-name sample-parameter-group
```
Output from this operation looks something like the following:
```
{
"Parameters": [
{
"ParameterName": "profiler_threshold_ms",
"ParameterValue": "100",
"Description": "Operations longer than profiler_threshold_ms will be logged",
"Source": "system",
"ApplyType": "dynamic",
"DataType": "integer",
"AllowedValues": "50-2147483646",
"IsModifiable": true,
"ApplyMethod": "pending-reboot"
},
{
"ParameterName": "tls",
"ParameterValue": "disabled",
"Description": "Config to enable/disable TLS",
"Source": "user",
"ApplyType": "static",
"DataType": "string",
"AllowedValues": "disabled,enabled",
"IsModifiable": true,
"ApplyMethod": "pending-reboot"
}
]
}
```
After determining the value of your tls
parameter, continue connecting to your cluster by using one of the code examples in the following sections.
Connecting with TLS Enabled
To view a code example for programmatically connecting to a TLS-enabled Amazon DocumentDB cluster, choose the appropriate tab for the language that you want to use.
To encrypt data in transit, download the public key for Amazon DocumentDB named rds-combined-ca-bundle.pem
using the following operation.
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
Python
The following code demonstrates how to connect to Amazon DocumentDB using Python when TLS is enabled.
import pymongo
import sys
##Create a MongoDB client, open a connection to Amazon DocumentDB as a replica set and specify the read preference as secondary preferred
client = pymongo.MongoClient('mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred')
##Specify the database to be used
db = client.sample_database
##Specify the collection to be used
col = db.sample_collection
##Insert a single document
col.insert_one({'hello':'Amazon DocumentDB'})
##Find the document that was previously written
x = col.find_one({'hello':'Amazon DocumentDB'})
##Print the result to the screen
print(x)
##Close the connection
client.close()
Node.js
The following code demonstrates how to connect to Amazon DocumentDB using Node.js when TLS is enabled.
var MongoClient = require('mongodb').MongoClient,
f = require('util').format,
fs = require('fs');
//Specify the Amazon DocumentDB cert
var ca = [fs.readFileSync("rds-combined-ca-bundle.pem")];
//Create a MongoDB client, open a connection to Amazon DocumentDB as a replica set,
// and specify the read preference as secondary preferred
var client = MongoClient.connect(
'mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/sample-database?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred',
{
sslValidate: true,
sslCA:ca,
useNewUrlParser: true
},
function(err, client) {
if(err)
throw err;
//Specify the database to be used
db = client.db('sample-database');
//Specify the collection to be used
col = db.collection('sample-collection');
//Insert a single document
col.insertOne({'hello':'Amazon DocumentDB'}, function(err, result){
//Find the document that was previously written
col.findOne({'hello':'Amazon DocumentDB'}, function(err, result){
//Print the result to the screen
console.log(result);
//Close the connection
client.close()
});
});
});
PHP
The following code demonstrates how to connect to Amazon DocumentDB using PHP when TLS is enabled.
<?php
//Include Composer's autoloader
require 'vendor/autoload.php';
$SSL_DIR = "/home/ubuntu";
$SSL_FILE = "rds-combined-ca-bundle.pem";
//Specify the Amazon DocumentDB cert
$ctx = stream_context_create(array(
"ssl" => array(
"cafile" => $SSL_DIR . "/" . $SSL_FILE,
))
);
//Create a MongoDB client and open connection to Amazon DocumentDB
$client = new MongoDB\Client("mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017", array("ssl" => true), array("context" => $ctx));
//Specify the database and collection to be used
$col = $client->sample-database->sample-collection;
//Insert a single document
$result = $col->insertOne( [ 'hello' => 'Amazon DocumentDB'] );
//Find the document that was previously written
$result = $col->findOne(array('hello' => 'Amazon DocumentDB'));
//Print the result to the screen
print_r($result);
?>
Go
The following code demonstrates how to connect to Amazon DocumentDB using Go when TLS is enabled.
Note
As of version 1.2.1, the MongoDB Go Driver will only use the first CA server certificate found in sslcertificateauthorityfile
. The example code below addresses this limitation by manually appending all server certificates found in sslcertificateauthorityfile
to a custom TLS configuration used during client creation.
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io/ioutil"
"crypto/tls"
"crypto/x509"
"errors"
)
const (
// Path to the AWS CA file
caFilePath = "rds-combined-ca-bundle.pem"
// Timeout operations after N seconds
connectTimeout = 5
queryTimeout = 30
username = "<sample-user>"
password = "<password>"
clusterEndpoint = "sample-cluster.node.us-east-1.docdb.amazonaws.com:27017"
// Which instances to read from
readPreference = "secondaryPreferred"
connectionStringTemplate = "mongodb://%s:%s@%s/sample-database?ssl=true&replicaSet=rs0&readpreference=%s"
)
func main() {
connectionURI := fmt.Sprintf(connectionStringTemplate, username, password, clusterEndpoint, readPreference)
tlsConfig, err := getCustomTLSConfig(caFilePath)
if err != nil {
log.Fatalf("Failed getting TLS configuration: %v", err)
}
client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI).SetTLSConfig(tlsConfig))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), connectTimeout*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatalf("Failed to connect to cluster: %v", err)
}
// Force a connection to verify our connection string
err = client.Ping(ctx, nil)
if err != nil {
log.Fatalf("Failed to ping cluster: %v", err)
}
fmt.Println("Connected to DocumentDB!")
collection := client.Database("sample-database").Collection("sample-collection")
ctx, cancel = context.WithTimeout(context.Background(), queryTimeout*time.Second)
defer cancel()
res, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
if err != nil {
log.Fatalf("Failed to insert document: %v", err)
}
id := res.InsertedID
log.Printf("Inserted document ID: %s", id)
ctx, cancel = context.WithTimeout(context.Background(), queryTimeout*time.Second)
defer cancel()
cur, err := collection.Find(ctx, bson.D{})
if err != nil {
log.Fatalf("Failed to run find query: %v", err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
log.Printf("Returned: %v", result)
if err != nil {
log.Fatal(err)
}
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
func getCustomTLSConfig(caFile string) (*tls.Config, error) {
tlsConfig := new(tls.Config)
certs, err := ioutil.ReadFile(caFile)
if err != nil {
return tlsConfig, err
}
tlsConfig.RootCAs = x509.NewCertPool()
ok := tlsConfig.RootCAs.AppendCertsFromPEM(certs)
if !ok {
return tlsConfig, errors.New("Failed parsing pem file")
}
return tlsConfig, nil
}
Java
When connecting to a TLS-enabled Amazon DocumentDB cluster from a Java application, your program must use the AWS-provided certificate authority (CA) file to validate the connection. To use the Amazon RDS CA certificate, do the following:
Download the Amazon RDS CA file from https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem .
Create a trust store with the CA certificate contained in the file by performing the following commands. Be sure to change the
<truststorePassword>
to something else. If you are accessing a trust store that contains both the old CA certificate (rds-ca-2015-root.pem
) and the new CA certificate (rds-ca-2019-root.pem
), you can import the certificate bundle into the trust store.The following is a sample shell script that imports the certificate bundle into a trust store on a Linux operating system.
mydir=/tmp/certs
truststore=${mydir}/rds-truststore.jks
storepassword=<truststorePassword>
curl -sS "https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem" > ${mydir}/rds-combined-ca-bundle.pem
awk 'split_after == 1 {n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1}{print > "rds-ca-" n ".pem"}' < ${mydir}/rds-combined-ca-bundle.pem
for CERT in rds-ca-*; do
alias=$(openssl x509 -noout -text -in $CERT | perl -ne 'next unless /Subject:/; s/.*(CN=|CN = )//; print')
echo "Importing $alias"
keytool -import -file ${CERT} -alias "${alias}" -storepass ${storepassword} -keystore ${truststore} -noprompt
rm $CERT
done
rm ${mydir}/rds-combined-ca-bundle.pem
echo "Trust store content is: "
keytool -list -v -keystore "$truststore" -storepass ${storepassword} | grep Alias | cut -d " " -f3- | while read alias
do
expiry=`keytool -list -v -keystore "$truststore" -storepass ${storepassword} -alias "${alias}" | grep Valid | perl -ne 'if(/until: (.*?)\n/) { print "$1\n"; }'`
echo " Certificate ${alias} expires in '$expiry'"
done
The following is a sample shell script that imports the certificate bundle into a trust store on macOS.
mydir=/tmp/certs
truststore=${mydir}/rds-truststore.jks
storepassword=<truststorePassword>
curl -sS "https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem" > ${mydir}/rds-combined-ca-bundle.pem
split -p "-----BEGIN CERTIFICATE-----" ${mydir}/rds-combined-ca-bundle.pem rds-ca-
for CERT in rds-ca-*; do
alias=$(openssl x509 -noout -text -in $CERT | perl -ne 'next unless /Subject:/; s/.*(CN=|CN = )//; print')
echo "Importing $alias"
keytool -import -file ${CERT} -alias "${alias}" -storepass ${storepassword} -keystore ${truststore} -noprompt
rm $CERT
done
rm ${mydir}/rds-combined-ca-bundle.pem
echo "Trust store content is: "
keytool -list -v -keystore "$truststore" -storepass ${storepassword} | grep Alias | cut -d " " -f3- | while read alias
do
expiry=`keytool -list -v -keystore "$truststore" -storepass ${storepassword} -alias "${alias}" | grep Valid | perl -ne 'if(/until: (.*?)\n/) { print "$1\n"; }'`
echo " Certificate ${alias} expires in '$expiry'"
done
Use the
keystore
in your program by setting the following system properties in your application before making a connection to the Amazon DocumentDB cluster.javax.net.ssl.trustStore: <truststore>
javax.net.ssl.trustStorePassword: <truststorePassword>
The following code demonstrates how to connect to Amazon DocumentDB using Java when TLS is enabled.
``` package com.example.documentdb;
import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.ServerAddress; import com.mongodb.MongoException; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import org.bson.Document;
public final class Main {
private Main() {
}
public static void main(String[] args) {
String template = "mongodb://%s:%s@%s/sample-database?ssl=true&replicaSet=rs0&readpreference=%s";
String username = "<sample-user>";
String password = "<password>";
String clusterEndpoint = "sample-cluster.node.us-east-1.docdb.amazonaws.com:27017";
String readPreference = "secondaryPreferred";
String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);
String truststore = "<truststore>";
String truststorePassword = "<truststorePassword>";
System.setProperty("javax.net.ssl.trustStore", truststore);
System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
MongoClientURI clientURI = new MongoClientURI(connectionString);
MongoClient mongoClient = new MongoClient(clientURI);
MongoDatabase testDB = mongoClient.getDatabase("sample-database");
MongoCollection<Document> numbersCollection = testDB.getCollection("sample-collection");
Document doc = new Document("name", "pi").append("value", 3.14159);
numbersCollection.insertOne(doc);
MongoCursor<Document> cursor = numbersCollection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}
}
```
C# / .NET
The following code demonstrates how to connect to Amazon DocumentDB using C# / .NET when TLS is enabled.
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using MongoDB.Driver;
using MongoDB.Bson;
namespace DocDB
{
class Program
{
static void Main(string[] args)
{
string template = "mongodb://{0}:{1}@{2}/sample-database?ssl=true&replicaSet=rs0&readpreference={3}";
string username = "<sample-user>";
string password = "<password>";
string readPreference = "secondaryPreferred";
string clusterEndpoint="sample-cluster.node.us-east-1.docdb.amazonaws.com:27017";
string connectionString = String.Format(template, username, password, clusterEndpoint, readPreference);
string pathToCAFile = "<path_to_rds-combined-ca-bundle.p7b_file>";
// ADD CA certificate to local trust store
// DO this once - Maybe when your service starts
X509Store localTrustStore = new X509Store(StoreName.Root);
X509Certificate2Collection certificateCollection = new X509Certificate2Collection();
certificateCollection.Import(pathToCAFile);
try
{
localTrustStore.Open(OpenFlags.ReadWrite);
localTrustStore.AddRange(certificateCollection);
}
catch (Exception ex)
{
Console.WriteLine("Root certificate import failed: " + ex.Message);
throw;
}
finally
{
localTrustStore.Close();
}
var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
var client = new MongoClient(settings);
var database = client.GetDatabase("sample-database");
var collection = database.GetCollection<BsonDocument>("sample-collection");
var docToInsert = new BsonDocument { { "pi", 3.14159 } };
collection.InsertOne(docToInsert);
}
}
}
mongo shell
The following code demonstrates how to connect to and query Amazon DocumentDB using the mongo shell when TLS is enabled.
Connect to Amazon DocumentDB with the mongo shell.
mongo --ssl --host sample-cluster.node.us-east-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username <sample-user> --password <password>
Insert a single document.
db.myTestCollection.insertOne({'hello':'Amazon DocumentDB'})
Find the document that was previously inserted.
db.myTestCollection.find({'hello':'Amazon DocumentDB'})
R
The following code demonstrates how to connect to Amazon DocumentDB with R using mongolite (https://jeroen.github.io/mongolite/) when TLS is enabled.
#Include the mongolite library.
library(mongolite)
mongourl <- paste("mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/test2?ssl=true&",
"readPreference=secondaryPreferred&replicaSet=rs0", sep="")
#Create a MongoDB client, open a connection to Amazon DocumentDB as a replica
# set and specify the read preference as secondary preferred
client <- mongo(url = mongo(url = mongourl, options = ssl_options(weak_cert_validation = F, ca = <path to 'rds-combined-ca-bundle.pem'>))
#Insert a single document
str <- c('{"hello" : "Amazon DocumentDB"}')
client$insert(str)
#Find the document that was previously written
client$find()
Ruby
The following code demonstrates how to connect to Amazon DocumentDB with Ruby when TLS is enabled.
require 'mongo'
require 'neatjson'
require 'json'
client_host = 'mongodb://sample-cluster.node.us-east-1.docdb.amazonaws.com:27017'
client_options = {
database: 'test',
replica_set: 'rs0',
read: {:secondary_preferred => 1},
user: '<sample-user>',
password: '<password>',
ssl: true,
ssl_verify: true,
ssl_ca_cert: <path to 'rds-combined-ca-bundle.pem'>
}
begin
##Create a MongoDB client, open a connection to Amazon DocumentDB as a
## replica set and specify the read preference as secondary preferred
client = Mongo::Client.new(client_host, client_options)
##Insert a single document
x = client[:test].insert_one({"hello":"Amazon DocumentDB"})
##Find the document that was previously written
result = client[:test].find()
#Print the document
result.each do |document|
puts JSON.neat_generate(document)
end
end
#Close the connection
client.close
Connecting with TLS Disabled
To view a code example for programmatically connecting to a TLS-disabled Amazon DocumentDB cluster, choose the tab for language that you want to use.
Python
The following code demonstrates how to connect to Amazon DocumentDB using Python when TLS is disabled.
## Create a MongoDB client, open a connection to Amazon DocumentDB as a replica set and specify the read preference as secondary preferred
client = pymongo.MongoClient('mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred')
##Specify the database to be used
db = client.sample_database
##Specify the collection to be used
col = db.sample_collection
##Insert a single document
col.insert_one({'hello':'Amazon DocumentDB'})
##Find the document that was previously written
x = col.find_one({'hello':'Amazon DocumentDB'})
##Print the result to the screen
print(x)
##Close the connection
client.close()
Node.js
The following code demonstrates how to connect to Amazon DocumentDB using Node.js when TLS is disabled.
var MongoClient = require('mongodb').MongoClient;
//Create a MongoDB client, open a connection to Amazon DocumentDB as a replica set,
// and specify the read preference as secondary preferred
var client = MongoClient.connect(
'mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/sample-database?replicaSet=rs0&readPreference=secondaryPreferred',
{
useNewUrlParser: true
},
function(err, client) {
if(err)
throw err;
//Specify the database to be used
db = client.db('sample-database');
//Specify the collection to be used
col = db.collection('sample-collection');
//Insert a single document
col.insertOne({'hello':'Amazon DocumentDB'}, function(err, result){
//Find the document that was previously written
col.findOne({'hello':'Amazon DocumentDB'}, function(err, result){
//Print the result to the screen
console.log(result);
//Close the connection
client.close()
});
});
});
PHP
The following code demonstrates how to connect to Amazon DocumentDB using PHP when TLS is disabled.
<?php
//Include Composer's autoloader
require 'vendor/autoload.php';
//Create a MongoDB client and open connection to Amazon DocumentDB
$client = new MongoDB\Client("mongodb://<sample-user>:<password>@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017");
//Specify the database and collection to be used
$col = $client->sample-database->sample-collection;
//Insert a single document
$result = $col->insertOne( [ 'hello' => 'Amazon DocumentDB'] );
//Find the document that was previously written
$result = $col->findOne(array('hello' => 'Amazon DocumentDB'));
//Print the result to the screen
print_r($result);
?>
Go
The following code demonstrates how to connect to Amazon DocumentDB using Go when TLS is disabled.
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
// Timeout operations after N seconds
connectTimeout = 5
queryTimeout = 30
username = "<sample-user>"
password = "<password>"
clusterEndpoint = "sample-cluster.node.us-east-1.docdb.amazonaws.com:27017"
// Which instances to read from
readPreference = "secondaryPreferred"
connectionStringTemplate = "mongodb://%s:%s@%s/sample-database?replicaSet=rs0&readpreference=%s"
)
func main() {
connectionURI := fmt.Sprintf(connectionStringTemplate, username, password, clusterEndpoint, readPreference)
client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), connectTimeout*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatalf("Failed to connect to cluster: %v", err)
}
// Force a connection to verify our connection string
err = client.Ping(ctx, nil)
if err != nil {
log.Fatalf("Failed to ping cluster: %v", err)
}
fmt.Println("Connected to DocumentDB!")
collection := client.Database("sample-database").Collection("sample-collection")
ctx, cancel = context.WithTimeout(context.Background(), queryTimeout*time.Second)
defer cancel()
res, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
if err != nil {
log.Fatalf("Failed to insert document: %v", err)
}
id := res.InsertedID
log.Printf("Inserted document ID: %s", id)
ctx, cancel = context.WithTimeout(context.Background(), queryTimeout*time.Second)
defer cancel()
cur, err := collection.Find(ctx, bson.D{})
if err != nil {
log.Fatalf("Failed to run find query: %v", err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
log.Printf("Returned: %v", result)
if err != nil {
log.Fatal(err)
}
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
Java
The following code demonstrates how to connect to Amazon DocumentDB using Java when TLS is disabled.
package com.example.documentdb;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
public final class Main {
private Main() {
}
public static void main(String[] args) {
String template = "mongodb://%s:%s@%s/sample-database?replicaSet=rs0&readpreference=%s";
String username = "<sample-user>";
String password = "<password>";
String clusterEndpoint = "sample-cluster.node.us-east-1.docdb.amazonaws.com:27017";
String readPreference = "secondaryPreferred";
String connectionString = String.format(template, username, password, clusterEndpoint, readPreference);
MongoClientURI clientURI = new MongoClientURI(connectionString);
MongoClient mongoClient = new MongoClient(clientURI);
MongoDatabase testDB = mongoClient.getDatabase("sample-database");
MongoCollection<Document> numbersCollection = testDB.getCollection("sample-collection");
Document doc = new Document("name", "pi").append("value", 3.14159);
numbersCollection.insertOne(doc);
MongoCursor<Document> cursor = numbersCollection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}
}
C# / .NET
The following code demonstrates how to connect to Amazon DocumentDB using C# / .NET when TLS is disabled.
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using MongoDB.Driver;
using MongoDB.Bson;
namespace CSharpSample
{
class Program
{
static void Main(string[] args)
{
string template = "mongodb://{0}:{1}@{2}/sample-database?&replicaSet=rs0&readpreference={3}";
string username = "<sample-user>";
string password = "<password>";
string clusterEndpoint = "sample-cluster.node.us-east-1.docdb.amazonaws.com:27017";
string readPreference = "secondaryPreferred";
string connectionString = String.Format(template, username, password, clusterEndpoint, readPreference);
var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
var client = new MongoClient(settings);
var database = client.GetDatabase("sample-database");
var collection = database.GetCollection<BsonDocument>("sample-collection");
var docToInsert = new BsonDocument { { "pi", 3.14159 } };
collection.InsertOne(docToInsert);
}
}
}
mongo shell
The following code demonstrates how to connect to and query Amazon DocumentDB using the mongo shell when TLS is disabled.
Connect to Amazon DocumentDB with the mongo shell.
mongo --host mycluster.node.us-east-1.docdb.amazonaws.com:27017 --username <sample-user> --password <password>
Insert a single document.
db.myTestCollection.insertOne({'hello':'Amazon DocumentDB'})
Find the document that was previously inserted.
db.myTestCollection.find({'hello':'Amazon DocumentDB'})
R
The following code demonstrates how to connect to Amazon DocumentDB with R using mongolite (https://jeroen.github.io/mongolite/) when TLS is disabled.
#Include the mongolite library.
library(mongolite)
#Create a MongoDB client, open a connection to Amazon DocumentDB as a replica
# set and specify the read preference as secondary preferred
client <- mongo(url = "mongodb://sample-user;:password@sample-cluster.node.us-east-1.docdb.amazonaws.com:27017/sample-database?readPreference=secondaryPreferred&replicaSet=rs0")
##Insert a single document
str <- c('{"hello" : "Amazon DocumentDB"}')
client$insert(str)
##Find the document that was previously written
client$find()
Ruby
The following code demonstrates how to connect to Amazon DocumentDB with Ruby when TLS is disabled.
require 'mongo'
require 'neatjson'
require 'json'
client_host = 'mongodb://sample-cluster.node.us-east-1.docdb.amazonaws.com:27017'
client_options = {
database: 'test',
replica_set: 'rs0',
read: {:secondary_preferred => 1},
user: '<sample-user>',
password: '<password>',
ssl: true,
ssl_verify: true,
ssl_ca_cert: <path to 'rds-combined-ca-bundle.pem'>
}
begin
##Create a MongoDB client, open a connection to Amazon DocumentDB as a
## replica set and specify the read preference as secondary preferred
client = Mongo::Client.new(client_host, client_options)
##Insert a single document
x = client[:test].insert_one({"hello":"Amazon DocumentDB"})
##Find the document that was previously written
result = client[:test].find()
#Print the document
result.each do |document|
puts JSON.neat_generate(document)
end
end
#Close the connection
client.close