Add doc example for finding invalid/corrupt documents

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Docs
    • 🔵 Done
    • Fully Compatible
    • Needed
    • Hide
      1. What would you like to communicate to the user about this feature?
        A way to deduce the invalid/corrupt document

      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      Yes
      3. Which versions of the driver/connector does this apply to?
      All

      Show
      What would you like to communicate to the user about this feature? A way to deduce the invalid/corrupt document 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes 3. Which versions of the driver/connector does this apply to? All
    • None
    • None
    • None
    • None
    • None
    • None

      Rarely, a user will run into an InvalidBSON exception when reading documents. When this happens it can be difficult to determine the problematic document(s). We should add a docs example for this scenario. One way is to use RawBSONDocument to find which specific document is invalid/corrupted:

      import bson
      
      raw_coll = coll.with_options(codec_options=coll.codec_options.with_options(document_class=bson.raw_bson.RawBSONDocument))
      for doc in raw_coll.find():
          try:
              bson.decode(doc.raw)
          except bson.errors.BSONError as exc:
              print(f"Invalid document {exc}, raw bson: {doc.raw}")
      

      RawBSONDocument lets them delay the BSON decoding so they can narrow down the problematic documents.

      Or, if the InvalidBSON error is coming from database.command:

      res = client.admin.command("serverStatus", codec_options=bson.raw_bson.DEFAULT_RAW_BSON_OPTIONS)
      try:
          bson.decode(res.raw)
      except bson.errors.BSONError as exc:
          print(f"Invalid BSON found in serverStatus response {exc}, raw bson: {res.raw}")
      

            Assignee:
            Jib Adegunloye
            Reporter:
            Shane Harvey
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: