# tfio.experimental.mongodb.MongoDBWriter

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/io/blob/v0.24.0/tensorflow_io/python/experimental/mongodb_writer_ops.py#L24-L98) |

Write documents to mongoDB.

    tfio.experimental.mongodb.MongoDBWriter(
        uri, database, collection
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Tensorflow datasets from MongoDB collections](https://www.tensorflow.org/io/tutorials/mongodb) |

The writer can be used to store documents in mongoDB while dealing with tensorflow
based models and inference outputs. Without loss of generality, consider an ML
model that is being used for inference. The outputs of inference can be modelled into
a structured record by enriching the schema with additional information( for ex: metadata
about input data and the semantics of the inference etc.) and can be stored in mongo
collections for persistence or future analysis.

To make a connection and write the documents to the mongo collections,
the [`tfio.experimental.mongodb.MongoDBWriter`](https://www.tensorflow.org/io/api_docs/python/tfio/experimental/mongodb/MongoDBWriter) API can be used.

#### Example:

    URI = "mongodb://mongoadmin:default_password@localhost:27017"
    DATABASE = "tfiodb"
    COLLECTION = "test"
    writer = tfio.experimental.mongodb.MongoDBWriter(
        uri=URI, database=DATABASE, collection=COLLECTION
    )
    for i in range(1000):
       data = {"key{}".format(i): "value{}".format(i)}
       writer.write(data)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `uri` | A string, representing the uri of the mongo server or replicaset. To connect to a MongoDB server with username and password based authentication, the following uri pattern can be used. Ex: `"mongodb://mongoadmin:default_password@localhost:27017"`. <br /> Connecting to a replica set is much like connecting to a standalone MongoDB server. Simply specify the replica set name using the `?replicaSet=myreplset` URI option. Ex: "mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset" Connection to a secure cluster via CA certs can be achieved by setting the respective TLS options to the URI. Ex: "mongodb://host01:27017/?tls=true\&sslCertificateAuthorityFile=/opt/ca.pem" Additional information on writing uri's can be found here: - [libmongoc uri docs](http://mongoc.org/libmongoc/current/mongoc_uri_t.html) - [mongodb uri docs](https://docs.mongodb.com/manual/reference/connection-string/) |
| `database` | A string, representing the database in the standalone MongoDB server or a replica set to connect to. |
| `collection` | A string, representing the collection from which the documents have to be retrieved. |

## Methods

### `write`

[View source](https://github.com/tensorflow/io/blob/v0.24.0/tensorflow_io/python/experimental/mongodb_writer_ops.py#L86-L91)

    write(
        doc
    )

Insert a single json document