<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Data Preservation Task 3
This experiment takes a dataset about divorces per year after marrige (link
Step1:  1. Connect to mongoDB
Establish a connection to the local mongoDB Docker container
Step2:  2. Fetch & Transform
Perform the following steps for the transformation
Step3:  Transform to JSON for pandas import
Step4:  Plot
Parse JSON into a pandas DataFrame object and plot as bar chart
<ASSISTANT_TASK:>
Python Code:
import pymongo
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import json
import re
from pymongo import MongoClient
%matplotlib inline
Explanation: Data Preservation Task 3
This experiment takes a dataset about divorces per year after marrige (link: https://www.data.gv.at/katalog/dataset/7fa00c8b-6189-42b8-af93-cc1ebff0a818) and plots the number of divorces per year from 1985 to 2014, for marriges that held between ten and eleven years.
The experiment consists of three steps:
Connect to mongodb
Fetch and transform data
Plot results
Imports:
End of explanation
client = MongoClient('mongodb')
db = client.dp
collection = db.divorce
Explanation: 1. Connect to mongoDB
Establish a connection to the local mongoDB Docker container
End of explanation
data = db.divorce.find()[0]['data']
for entry in data:
    entry['DIVORCES'] = entry['values'][0]['NUMBER']
    s = entry['DURATION']
    tmp = re.findall(r'\d+', s)
    if (len(tmp) == 1):
        tmp[0] = 0
    del entry['values']
    del entry['NUTS1']
    del entry['NUTS2']
    entry['DURATION'] = tmp[0]
Explanation: 2. Fetch & Transform
Perform the following steps for the transformation:
  * For each entry, gather the number of divorces from the unnecessarily nested values object
  * The DURATION field contains a string of the form "x to under y years". Parse the first value x
  * Delete all other attributes of an entry, except REF_YEAR
End of explanation
data_json = json.dumps(data)
Explanation: Transform to JSON for pandas import:
End of explanation
df = pd.read_json(data_json)
filtered = df[df.DURATION == 10].filter(items=['DIVORCES','REF_YEAR'])
filtered
filtered.plot.bar(x='REF_YEAR',y='DIVORCES')
Explanation: Plot
Parse JSON into a pandas DataFrame object and plot as bar chart
End of explanation
<END_TASK>