title: Add dictionary to MongoDB from Flask
body: <p>I have a function in Python Flask to add my data in MongoDB. Date data being collected from form is in text format but I want to save it in date format in MongoDB. So I am converting the format before inserting it to Mongo.</p>
  
  <pre><code>@app.route('/addtaskdb/', methods=["GET","POST"])
  def addtaskdb():
      try:
          print("***********entered in addtaskdb function ")
          if request.method == 'POST':
              data = request.form.to_dict()
              print(type(data))
              month,day, year = map(int, data['deliverydate'].split("/"))
              print(month)
              dbdate = datetime.date(year, month, day)                        
              data['deliverydate1'] = dbdate                        
              db.tasks.insert(data)
              return redirect(url_for('dashboard'))
      except Exception as e:
          flash(e)
          return redirect(url_for('dashboard'))
  </code></pre>
  
  <p>I am getting following error.</p>
  
  <pre><code>   builtins.TypeError
  TypeError: Object of type 'InvalidDocument' is not JSON serializable
  
  Traceback (most recent call last)
  File "C:\New folder\lib\site-packages\flask\app.py", line 1997, in __call__
  return self.wsgi_app(environ, start_response)
  File "C:\New folder\lib\site-packages\flask\app.py", line 1985, in wsgi_app
  response = self.handle_exception(e)
  File "C:\New folder\lib\site-packages\flask\app.py", line 1540, in handle_exception
  reraise(exc_type, exc_value, tb)
  File "C:\New folder\lib\site-packages\flask\_compat.py", line 33, in reraise
  raise value
  File "C:\New folder\lib\site-packages\flask\app.py", line 1982, in wsgi_app
  response = self.full_dispatch_request()
  File "C:\New folder\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
  return self.finalize_request(rv)
  File "C:\New folder\lib\site-packages\flask\app.py", line 1632, in finalize_request
  response = self.process_response(response)
  File "C:\New folder\lib\site-packages\flask\app.py", line 1858, in process_response
  self.save_session(ctx.session, response)
  File "C:\New folder\lib\site-packages\flask\app.py", line 924, in save_session
  return self.session_interface.save_session(self, session, response)
  File "C:\New folder\lib\site-packages\flask\sessions.py", line 363, in save_session
  val = self.get_signing_serializer(app).dumps(dict(session))
  File "C:\New folder\lib\site-packages\itsdangerous.py", line 565, in dumps
  payload = want_bytes(self.dump_payload(obj))
  File "C:\New folder\lib\site-packages\itsdangerous.py", line 847, in dump_payload
  json = super(URLSafeSerializerMixin, self).dump_payload(obj)
  File "C:\New folder\lib\site-packages\itsdangerous.py", line 550, in dump_payload
  return want_bytes(self.serializer.dumps(obj))
  File "C:\New folder\lib\site-packages\flask\sessions.py", line 85, in dumps
  return json.dumps(_tag(value), separators=(',', ':'))
  File "C:\New folder\lib\site-packages\flask\json.py", line 123, in dumps
  rv = _json.dumps(obj, **kwargs)
  File "C:\New folder\lib\json\__init__.py", line 238, in dumps
  **kw).encode(obj)
  File "C:\New folder\lib\json\encoder.py", line 199, in encode
  chunks = self.iterencode(o, _one_shot=True)
  File "C:\New folder\lib\json\encoder.py", line 257, in iterencode
  return _iterencode(o, 0)
  File "C:\New folder\lib\site-packages\flask\json.py", line 80, in default
  return _json.JSONEncoder.default(self, o)
  File "C:\New folder\lib\json\encoder.py", line 180, in default
  o.__class__.__name__)
  TypeError: Object of type 'InvalidDocument' is not JSON serializable
  </code></pre>
  
  <p>How can I  add date in MongoDB with my current setup?</p>