# Cannot export Date fields to MongoDB

**URL:** https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943
**Category:** Logstash
**Created:** [July 30, 2019, 4:11pm UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943 "2019-07-30T16:11:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [July 30, 2019, 4:11pm UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/1 "2019-07-30T16:11:34Z")

</div>

Hello.

I'm facing an issue when I try to write date fields to MongoDB, I've tried to convert it in many ways but with no success.

In my input JSON I have a collection like this:

```

"Colecs": [
		{
			"Value": {
				"Value": "Tester"
			},
			"FieldName": {
				"Value": "Name"
			},
			"Type": {
				"Value": "String"
			}
		},
		{
			"FieldName": {
				"Value": "DoB"
			},
			"Value": {
				"Value": "7/17/2019 3:58:31 PM"
			},
			"Type": {
				"Value": "DateTime"
			}
		}
		{
			"FieldName": {
				"Value": "Height"
			},
			"Value": {
				"Value": "1.75"
			},
			"Type": {
				"Value": "Float"
			}
		},
	],

```

In my config I use this Ruby filter:

```

ruby{
	code => "

		require 'date'
		require 'time'

		event.get('[Colecs]').each do |item|

			varValue = item['Value']['Value']
			varFieldName = item['FieldName']['Value']
			varType = item['Type']['Value']

			if varType == 'Integer'
				event.set( varFieldName, varValue.to_i)
			elsif varType == 'Float'			
				event.set( varFieldName, varValue.to_f)

			elsif varType == 'DateTime'

				event.set( varFieldName, { 'date' => varValue} ) <---Here is where I tried to convert

			else
				event.set( varFieldName, varValue.to_s)
			end
		end
	"
}

```

For Output I'm using this:

```

mongodb {
uri => "mongodb://localhost:27017"
database => "TestMongo"
collection => "MyCollection"
#isodate => true
id => "writeInMongoDB"
codec => "json"
}

```

In MongoDB I get a collection like this:

```

{
	"_id": "5d3ec9a70ebee3db90d8c4f2",
	"Height": 1.75,
	"DoB": {
	      "date": "7/17/2019 3:58:31 PM"
	},
	"Name": "Tester",
	"@timestamp": "\"2019-07-29T10:25:42.344Z\""
}

```

What I can't do is to convert the [DoB][date] from string to date.  
How can I do it?

I've already tried to convert it on Ruby filter in many ways with no success.  
Using MongoDB-output isodate gives me an error, that's why it's commented.  
Using Logstash date filter have no effect, the field doesn't change from string.  
Got no success in every attempt.  
Any ideas?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 30, 2019, 4:17pm UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/2 "2019-07-30T16:17:49Z")

</div>

> [@HFolly](#):
>
> Using Logstash date filter have no effect

What was the configuration of the date filter?

---

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [July 30, 2019, 4:56pm UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/3 "2019-07-30T16:56:59Z")

</div>

If I try this:

```
date {
	match => ["[DoB][date]", "ISO8601" ]
}

```

I get:

```
date {
	"_id": "5d4072530ebee3c344edb4f5"
	"@timestamp": "\"2019-07-30T16:37:39.228Z\"",
	"DoB": {
		"date": "7/17/2019 3:58:31 PM"
	},
	"tags": [
		"_dateparsefailure"
	],
	"Height": 1.75,
	"Name": "Tester"
}

```

And if I try this:

```
date {
	match => ["[DoB][date]", "M/d/YYYY hh:mm:ss a" ]
}

```

I get:

```
{
	"_id": "5d4074520ebee32517168961",
	"Height": 1.75,
	"DoB": {
		"date": "7/17/2019 3:58:31 PM"
	},
	"Name": "Tester",
	"@timestamp": "\"2019-07-17T14:58:31.000Z\""
}

```

And finally, if I try this:

```
date {
	match => ["[DoB][date]", "M/d/YYYY hh:mm:ss a" ]
        target => "[DoB][date]"
}

```

I get an error:

[WARN][logstash.outputs.mongodb] Failed to send event to MongoDB, retrying in 30 seconds {:event=\>#LogStash::Event:0xd8bcf4, :exception=\>#\<ArgumentError: wrong number of arguments (2 for 1)\>}

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 30, 2019, 5:01pm UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/4 "2019-07-30T17:01:52Z")

</div>

Do you get a stack trace with that error message?

---

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [July 31, 2019, 8:08am UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/5 "2019-07-31T08:08:05Z")

</div>

Got this from Logstash log

---

<div class="post-metadata">

### Author: ![HFolly](https://avatars.discourse-cdn.com/v4/letter/h/ac8455/32.png) [@HFolly](https://discuss.elastic.co/u/HFolly)
#### Post date: [August 1, 2019, 9:35am UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/6 "2019-08-01T09:35:52Z")

</div>

For future reference, I solved my problem downgrading the logstash-output-mongodb from version 3.1.6 to 3.1.5

Now the date filter:

```
date {
	match => ["[DoB][date]", "M/d/YYYY hh:mm:ss a" ]
        target => "[DoB][date]"
}

```

is working and MongoDB is receiving the field as date (it wasn't necessary to use the MongoDB output isodate option).

Thank you @Badger for answering me!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 29, 2019, 9:35am UTC](https://discuss.elastic.co/t/cannot-export-date-fields-to-mongodb/192943/7 "2019-08-29T09:35:56Z")

</div>

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