You can stream change data capture (CDC) updates from self-managed MongoDB
replica sets or sharded clusters to a [supported
destination](https://docs.cloud.google.com/datastream/docs/destinations), such as BigQuery or
Cloud Storage, by using Datastream. To prepare your source, you
create a Datastream user with read privileges in the `admin`
database for your deployment.

## Replica set

For information about deploying a MongoDB replica set, see
[Deploy a self-managed replica set](https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/)
in the MongoDB documentation.

To configure Datastream for use with a self-managed MongoDB
replica set, follow these steps:

1. Install the MongoDB Shell. For information about installing the MongoDB Shell, see [Install
   mongosh](https://www.mongodb.com/docs/mongodb-shell/install/).
2. Open a terminal window, and run the `mongosh` command to connect to your replica set or primary node.
3. Create a user for Datastream in the `admin` database:

       use admin
       db.createUser({
         user: "USERNAME",
         pwd: "PASSWORD",
         roles: [ "readAnyDatabase", {role: "read", db: "admin"} ]
       })

   Replace <var translate="no">USERNAME</var> and <var translate="no">PASSWORD</var> with your preferred username and password.

## Sharded cluster

For information about deploying a sharded cluster, see
[Deploy a sharded cluster](https://www.mongodb.com/docs/kubernetes-operator/current/tutorial/deploy-sharded-cluster/)
in the MongoDB documentation.

To configure Datastream for use with a self-managed MongoDB
sharded cluster, follow these steps:

1. Install the MongoDB Shell. For information about installing the MongoDB Shell, see [Install mongosh](https://www.mongodb.com/docs/mongodb-shell/install/).
2. Open a terminal window, and run the `mongosh` command to connect to your MongoDB sharded cluster.
3. Create a Datastream user in every primary shard
   node and the `mongos` query router:

       use admin
       db.createUser({
          user: "USERNAME",
          pwd: "PASSWORD",
          roles: [ "readAnyDatabase" ]
       })

   Replace <var translate="no">USERNAME</var> and <var translate="no">PASSWORD</var> with your username and
   password. You can choose any username and password, but they must be identical
   across all primary shard nodes and the `mongos` query router.
4. To find out which shards are connected to the `mongos` query router, run the
   following command:

   `db.adminCommand({ listShards: 1 })`