@@ -348,11 +348,63 @@ user-defined struct by using methods from the ``bson`` package:
348348 You can use the ``Raw`` type to retrieve elements from a BSON
349349 document byte slice without unmarshalling it to a Go type. This type
350350 allows you to look up individual elements without unmarshalling
351- the entire BSON document.
351+ the entire BSON document. For more information about how to use the ``Raw`` type, see
352+ the :ref:`golang-bson-raw` section.
352353
353354To learn more about the marshalling and unmarshalling methods used with the
354- ``Cursor`` type, see the `Cursor API documentation <{+api+}/mongo#Cursor>`__
355+ ``Cursor`` type, see the `Cursor API documentation <{+api+}/mongo#Cursor>`__.
355356
356357To learn more about the marshalling and unmarshalling methods in the
357358``bson`` package, see the `bson API documentation
358- <{+api+}/bson#hdr-Marshalling_and_Unmarshalling>`_
359+ <{+api+}/bson#hdr-Marshalling_and_Unmarshalling>`_.
360+
361+ .. _golang-bson-raw:
362+
363+ Work with Raw BSON Data
364+ -----------------------
365+
366+ The {+driver-short+} supports working with raw BSON documents through the ``Raw`` type. The ``Raw``
367+ type allows you to validate and retrieve elements from a slice of bytes without unmarshalling
368+ the entire BSON document. This is useful in the following situations:
369+
370+ - Performing lookups on specific fields without converting the entire document
371+ - Reducing memory overhead by avoiding full document unmarshalling
372+ - Working directly with binary BSON data received from the database
373+
374+ The ``Raw`` type provides methods to validate the BSON document and look up individual
375+ elements by their keys. The following example demonstrates how to validate a raw BSON document and retrieve
376+ a specific field from it:
377+
378+ .. io-code-block::
379+ :copyable: true
380+
381+ .. input::
382+ :language: go
383+
384+ collection := client.Database("sample_restaurants").Collection("restaurants")
385+ findOptions := options.FindOne()
386+ var raw bson.Raw
387+ err = collection.FindOne(
388+ context.TODO(),
389+ bson.D{{"name", "Mongo's Pizza"}},
390+ findOptions,
391+ ).Decode(&raw)
392+ if err != nil {
393+ log.Fatalf("Failed to find document: %v", err)
394+ }
395+
396+ // Print the document type
397+ fmt.Printf("Document type: %T\n", raw)
398+
399+ // Access a field from the raw document
400+ name := raw.Lookup("name").StringValue()
401+ fmt.Println("Restaurant name:", name)
402+
403+ .. output::
404+ :language: none
405+ :visible: false
406+
407+ Document type: bson.Raw
408+ Restaurant name: Mongo's Pizza
409+
410+ To learn more about the ``Raw`` family of types, see the `Raw BSON API documentation <{+api+}/bson#hdr-Raw_BSON>`__.
0 commit comments