title: pymongo: Document must be an instance of dict
body: <p>Afternoon,
  I'm facing a problem with <strong>pymongo</strong>, I'm not able to set up correctly the parameter to insert into MongoDB via insert_many(). I came across with de following error:</p>
  <p><a href="https://i.stack.imgur.com/jKrWK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jKrWK.png" alt="Insert error" /></a></p>
  <blockquote>
  <p>TypeError: document must be an instance of dict, bson.son.SON,
  bson.raw_bson.RawBSONDocument, or a type that inherits from
  collections.MutableMapping [while running 'Insere no MongoDB']</p>
  </blockquote>
  <p>What am I doing wrong ?</p>
  <pre><code>class InsertMongoDB(beam.DoFn):
      def process(self, element):
          arqJson=json.loads(element)
  
          client = MongoClient(&quot;mongodb://user:password@mkp-cr-marketplace-core.lcr88.gcp.mongodb.net/db-poc-base360?retryWrites=true&amp;w=majority%20&quot;)
          db = client['db-poc-base360']
          db.tbPropostaSucesso.insert_many(arqJson)
  
          # tbPropostaErro = db['tbPropostaErro']
          # tbPropostaErro
  
          resultado = 0
  
          yield resultado
  </code></pre>
  <p>I receive a messagem from Google PubSub and forward it to a method called InsertMongoDB().
  I don't know how to suit my massage, whose value is in a json format, to use it correctly in the insert_many().</p>
  <p>When I debug my variable &quot;arqJson&quot; has :</p>
  <p><a href="https://i.stack.imgur.com/VLCXG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VLCXG.png" alt="![arqjson" /></a></p>
  <p>The json that I'm using is :</p>
  <pre><code>{
      &quot;Status&quot;: &quot;Sucesso &quot;,
      &quot;Documento&quot;: {
          &quot;Apolice&quot;: [{
              &quot;ItemAuto&quot;: [{
                  &quot;nmTipo&quot;: &quot;FOX&quot;,
                  &quot;nrItem&quot;: &quot;000001&quot;,
                  &quot;nmMarca&quot;: &quot;VOLKSWAGEN&quot;,
                  &quot;aaModelo&quot;: &quot;2017&quot;,
                  &quot;cdModelo&quot;: &quot;0017664&quot;,
                  &quot;nmModelo&quot;: &quot;TRENDLINE 1.0 FLEX 12V 5P&quot;,
                  &quot;aaFabricacao&quot;: &quot;2016&quot;,
                  &quot;nmTipoVeiculo&quot;: &quot;Hatch&quot;
              }, {
                  &quot;nmTipo&quot;: &quot;FOX&quot;,
                  &quot;nrItem&quot;: &quot;000001&quot;,
                  &quot;nmMarca&quot;: &quot;VOLKSWAGEN&quot;,
                  &quot;aaModelo&quot;: &quot;2017&quot;,
                  &quot;cdModelo&quot;: &quot;0017664&quot;,
                  &quot;nmModelo&quot;: &quot;TRENDLINE 1.0 FLEX 12V 5P&quot;,
                  &quot;aaFabricacao&quot;: &quot;2016&quot;,
                  &quot;nmTipoVeiculo&quot;: &quot;Hatch&quot;
              }],
              &quot;ItemProp&quot;: [{
                  &quot;dsUF&quot;: &quot;MG&quot;,
                  &quot;idLocal&quot;: &quot;000001&quot;,
                  &quot;dsCidade&quot;: &quot;BELO HORIZONTE&quot;,
                  &quot;dsEndereco&quot;: &quot;RUA RUA RUA&quot;,
                  &quot;dsComplemento&quot;: &quot;CASA&quot;
              }],
              &quot;cdEmpresa&quot;: &quot;1&quot;,
              &quot;idApolice&quot;: &quot;501741&quot;,
              &quot;idEndosso&quot;: &quot;000000&quot;,
              &quot;cdCarteira&quot;: &quot;431&quot;,
              &quot;cdSucursal&quot;: &quot;010&quot;,
              &quot;cdPatrimonio&quot;: &quot;1&quot;,
              &quot;nrItemContrato&quot;: &quot;2&quot;,
              &quot;dsTipoDocumento&quot;: &quot;A&quot;,
              &quot;cdVeiculoSegurado&quot;: &quot;1&quot;
          }],
          &quot;Cliente&quot;: [{
              &quot;cdCliente&quot;: &quot;1&quot;,
              &quot;nmCliente&quot;: &quot;Lucas&quot;,
              &quot;nrCpfCnpj&quot;: &quot;4355582833&quot;,
              &quot;icRegistroAtivo&quot;: &quot;1&quot;,
              &quot;cdAcaoInformacao&quot;: &quot;A&quot;,
              &quot;dtAcaoInformacao&quot;: &quot;2020-02-02&quot;,
              &quot;cdServicoAcaoInformacao&quot;: &quot;cdServicoAcao&quot;,
              &quot;cdUsuarioAcaoInformacao&quot;: &quot;cdUsuarioAcao&quot;
          }, {
              &quot;cdCliente&quot;: &quot;2&quot;,
              &quot;nmCliente&quot;: &quot;Lucas&quot;,
              &quot;nrCpfCnpj&quot;: &quot;43331971&quot;,
              &quot;icRegistroAtivo&quot;: &quot;1&quot;,
              &quot;cdAcaoInformacao&quot;: &quot;A&quot;,
              &quot;dtAcaoInformacao&quot;: &quot;2020-02-01&quot;,
              &quot;cdServicoAcaoInformacao&quot;: &quot;cdServicoAcao2&quot;,
              &quot;cdUsuarioAcaoInformacao&quot;: &quot;cdUsuarioAcao2&quot;
          }],
          &quot;Mensagem&quot;: [{
              &quot;cdMensagem&quot;: &quot;1&quot;,
              &quot;dsMensagem&quot;: &quot;Teste de mensagem&quot;
          }],
          &quot;EnderecoCobranca&quot;: [{
              &quot;dsUF&quot;: &quot;RS&quot;,
              &quot;dsBairro&quot;: &quot;INTEGRAÇÃO&quot;,
              &quot;dsCidade&quot;: &quot;PAROBE&quot;,
              &quot;cdEndereco&quot;: 1,
              &quot;dsEndereco&quot;: &quot;RUA RUA RUA&quot;,
              &quot;nrEndereco&quot;: &quot;280&quot;,
              &quot;dsComplemento&quot;: &quot;&quot;,
              &quot;icRegistroAtivo&quot;: &quot;1&quot;,
              &quot;cdAcaoInformacao&quot;: &quot;A&quot;,
              &quot;dtAcaoInformacao&quot;: &quot;2020-02-02&quot;,
              &quot;cdServicoAcaoInformacao&quot;: &quot;cdServicoAcao&quot;,
              &quot;cdUsuarioAcaoInformacao&quot;: &quot;cdUsuarioAcao&quot;
          }, {
              &quot;dsUF&quot;: &quot;SP&quot;,
              &quot;dsBairro&quot;: &quot;INTEGRAÇÃO2&quot;,
              &quot;dsCidade&quot;: &quot;POC2&quot;,
              &quot;cdEndereco&quot;: 2,
              &quot;dsEndereco&quot;: &quot;RUA B&quot;,
              &quot;nrEndereco&quot;: &quot;222&quot;,
              &quot;dsComplemento&quot;: &quot;CASA 2&quot;,
              &quot;icRegistroAtivo&quot;: &quot;1&quot;,
              &quot;cdAcaoInformacao&quot;: &quot;A&quot;,
              &quot;dtAcaoInformacao&quot;: &quot;2020-02-01&quot;,
              &quot;cdServicoAcaoInformacao&quot;: &quot;cdServicoAcao2&quot;,
              &quot;cdUsuarioAcaoInformacao&quot;: &quot;cdUsuarioAcao2&quot;
          }]
      }
  }
  </code></pre>
  <p><strong>2020/11/20:</strong></p>
  <p>At the moment I'm struggling with the format of arqJson that I need to use in the insert_one(arqJson).</p>
  <p><a href="https://i.stack.imgur.com/R9iXF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R9iXF.png" alt="Parameter format" /></a></p>
  <p>I forgot to mention that my method <strong>InsertMongoDB</strong> receives the arqJson from another method called <strong>InsertPostgreSQL</strong>.</p>
  <p>InsertPostgreSQL does:</p>
  <ul>
  <li>Receive the message from Pubsub;</li>
  <li>-Transform the element : json.dumps(json.loads(element))</li>
  <li>Save it into arqJson. After that, InsertMongoDB is called.
  At this moment, I don't know how to format &quot;element&quot; (whose type is list) and save it into arqJson, because I have this error:</li>
  </ul>
  <blockquote>
  <p>raise TypeError(&quot;%s must be an instance of dict, bson.son.SON, &quot;
  TypeError: document must be an instance of dict, bson.son.SON,
  bson.raw_bson.RawBSONDocument, or a type that inherits from
  collections.MutableMapping [while running 'Insere no MongoDB']</p>
  </blockquote>
  <p>Thank you,
  Juliano</p>