# Mongo sink remove null values fields from Document

**URL:** <https://forum.confluent.io/t/mongo-sink-remove-null-values-fields-from-document/1635>\
**Category:** Kafka Connect\
**Created:** [30 April 2021 10:17 UTC](https://forum.confluent.io/t/mongo-sink-remove-null-values-fields-from-document/1635 "2021-04-30T10:17:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![achyut](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@achyut](https://forum.confluent.io/u/achyut)\
**Post date:** [30 April 2021 10:17 UTC](https://forum.confluent.io/t/mongo-sink-remove-null-values-fields-from-document/1635/1 "2021-04-30T10:17:16Z")

</div>

I’m reading bin-logs data from MySQL using debezium and sink payload data to MongoDB using Kafka-MongoDB sink connector. Following the example of Kafka’s message.

> ```
> {
> "schema": {
> "type": "struct",
> "fields": [
> {
> "type": "int32",
> "optional": false,
> "field": "id"
> },
> {
> "type": "string",
> "optional": true,
> "field": "Name"
> },
> {
> "type": "string",
> "optional": true,
> "field": "City"
> },
> {
> "type": "string",
> "optional": true,
> "field": "Email"
> },
> {
> "type": "int64",
> "optional": true,
> "name": "io.debezium.time.Timestamp",
> "version": 1,
> "field": "Joindate"
> },
> {
> "type": "string",
> "optional": true,
> "field": "__table"
> },
> {
> "type": "string",
> "optional": true,
> "field": "__deleted"
> }
> ],
> "optional": false,
> "name": "Docker_Mysql.kafka_TEST.Testing.Value"
> },
> "payload": {
> "id": 4,
> "Name": "Sasha",
> "City": "Gresham",
> "Email": "arcu.Vestibulum.ante@elitpharetra.org",
> "Joindate": 1495964559000,
> "__table": "Testing",
> "__deleted": "false"
> }
> }
> 
> ```

> ```
> {
> "schema": {
> "type": "struct",
> "fields": [
> {
> "type": "int32",
> "optional": false,
> "field": "id"
> },
> {
> "type": "string",
> "optional": true,
> "field": "Name"
> },
> {
> "type": "string",
> "optional": true,
> "field": "City"
> },
> {
> "type": "string",
> "optional": true,
> "field": "Email"
> },
> {
> "type": "int64",
> "optional": true,
> "name": "io.debezium.time.Timestamp",
> "version": 1,
> "field": "Joindate"
> },
> {
> "type": "string",
> "optional": true,
> "field": "__table"
> },
> {
> "type": "string",
> "optional": true,
> "field": "__deleted"
> }
> ],
> "optional": false,
> "name": "Docker_Mysql.kafka_TEST.Testing.Value"
> },
> "payload": {
> "id": 11,
> "Name": "Test",
> "City": "Test",
> "Email": "Test@test.com",
> "Joindate": null,
> "__table": "Testing",
> "__deleted": "false"
> }
> }
> 
> ```

So the Mongo DB document is

```
{
  "_id": 4,
  "Name": "Sasha",
  "City": "Gresham",
  "Email": "arcu.Vestibulum.ante@elitpharetra.org",
  "Joindate": "1495964559000",
  "__table": "Testing",
  "__deleted": "false"
},
{
   "_id": 11,
   "Name": "Test",
   "City": "Test",
   "Email": "Test@test.com",
  "Joindate": null,
  "__table": "Testing",
   "__deleted": "false"
 }

```

but if any key has a null value then remove that key value from the document  
so a document with **\_id=11** should be like this to remove **“Joindate”** key value from mongo document.

> {  
> “\_id”: 11,  
> “Name”: “Test”,  
> “City”: “Test”,  
> “Email”: “[Test@test.com](mailto:Test@test.com)”,  
> “\_\_table”: “Testing”,  
> “\_\_deleted”: “false”  
> }

So how can I dynamically configure the connector to remove null from Mongo document?

Code from Mongo Shell

```
db.Testing.update(
  {},
  [{ $replaceWith: {
    $arrayToObject: {
      $filter: {
        input: { $objectToArray: "$$ROOT" },
        as: "item",
        cond: { $ne: ["$$item.v", null] }
      }
    }
  }}],
  { multi: true }
)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [30 May 2021 10:17 UTC](https://forum.confluent.io/t/mongo-sink-remove-null-values-fields-from-document/1635/2 "2021-05-30T10:17:52Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
