Skip to content

Commit 444d6bc

Browse files
author
Chris Cho
authored
DOCSP-22293: Records and Codec Provider (#280)
* DOCSP-22293: Records and Codec Provider
1 parent 5402511 commit 444d6bc

File tree

8 files changed

+150
-17
lines changed

8 files changed

+150
-17
lines changed

source/fundamentals/data-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Data Formats
88
- :doc:`/fundamentals/data-formats/document-data-format-extended-json`
99
- :doc:`/fundamentals/data-formats/documents`
1010
- :doc:`/fundamentals/data-formats/document-data-format-pojo`
11+
- :doc:`/fundamentals/data-formats/document-data-format-record`
1112
- :doc:`/fundamentals/data-formats/pojo-customization`
1213
- :doc:`/fundamentals/data-formats/codecs`
1314

@@ -18,6 +19,7 @@ Data Formats
1819
/fundamentals/data-formats/document-data-format-extended-json
1920
/fundamentals/data-formats/documents
2021
/fundamentals/data-formats/document-data-format-pojo
22+
/fundamentals/data-formats/document-data-format-record
2123
/fundamentals/data-formats/pojo-customization
2224
/fundamentals/data-formats/codecs
2325

source/fundamentals/data-formats/codecs.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _fundamentals-codecs:
2+
13
======
24
Codecs
35
======
@@ -32,7 +34,7 @@ sections:
3234
- :ref:`CodecProvider <codecs-codecprovider>`
3335
- :ref:`Custom Codec Example <codecs-custom-example>`
3436

35-
If you are customizing encoding and decoding logic for Plain old Java objects
37+
If you are customizing encoding and decoding logic for plain old Java objects
3638
(POJOs), read our guide on :doc:`POJO Customization </fundamentals/data-formats/pojo-customization>`.
3739

3840
.. _codecs-codec:

source/fundamentals/data-formats/document-data-format-pojo.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _fundamentals-pojos:
2+
13
===========================
24
Document Data Format: POJOs
35
===========================
@@ -21,7 +23,7 @@ data representation.
2123
The example in this guide shows how to perform the following:
2224

2325
- Configure the driver to serialize and deserialize POJOs
24-
- How to read and write to documents using POJOs
26+
- How to read and write to documents using POJOs
2527

2628
.. _fundamentals-example-pojo:
2729

@@ -120,8 +122,8 @@ To set up the driver to store and retrieve POJOs, we need to specify:
120122
encode/decode the data between the POJO and MongoDB document, and which
121123
POJO classes or packages that the codecs should apply to.
122124
- A ``CodecRegistry`` instance that contains the codecs and other related information.
123-
- A ``MongoClient``, ``MongoDatabase``, or ``MongoCollection`` instance
124-
configured to use the ``CodecRegistry``.
125+
- A ``MongoDatabase`` or ``MongoCollection`` instance configured to use the
126+
``CodecRegistry``.
125127
- A ``MongoCollection`` instance created with the POJO document class
126128
bound to the ``TDocument`` generic type.
127129

@@ -130,7 +132,7 @@ requirements:
130132

131133
1. Configure the ``PojoCodecProvider``. In this example, we use the ``automatic(true)``
132134
setting of the ``PojoCodecProvider.Builder`` to apply the Codecs to
133-
any class and its properties.
135+
any class and its properties.
134136

135137
.. code-block:: java
136138

@@ -140,7 +142,7 @@ requirements:
140142

141143
Codec providers also contain other objects such as ``ClassModel`` and
142144
``Convention`` instances that further define serialization behavior.
143-
For more information on codec providers and customization, see our guide
145+
For more information on codec providers and customization, see the guide
144146
on :doc:`POJO Customization </fundamentals/data-formats/pojo-customization>`.
145147

146148
#. Add the ``PojoCodecProvider`` instance to a ``CodecRegistry``. The
@@ -166,11 +168,10 @@ requirements:
166168

167169
CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
168170

169-
#. Configure our ``MongoClient``, ``MongoDatabase``, or ``MongoCollection``
170-
instance to use the Codecs in the ``CodecRegistry``. You only need to
171-
configure one of these. In this example, we set the ``CodecRegistry`` on a
172-
``MongoDatabase`` called ``sample_pojos`` using the ``withCodecRegistry()``
173-
method.
171+
#. Configure the ``MongoDatabase`` or ``MongoCollection`` instance to use the
172+
Codecs in the ``CodecRegistry``. You only need to configure one of these.
173+
In this example, we set the ``CodecRegistry`` on a ``MongoDatabase`` called
174+
``sample_pojos`` using the ``withCodecRegistry()`` method.
174175

175176
.. code-block:: java
176177

@@ -220,10 +221,10 @@ When you run this code, your output should look something like this:
220221

221222
By default, the ``PojoCodecProvider`` omits fields in your POJO that are
222223
set to ``null``. For more information on how to specify this behavior, see
223-
our guide on :doc:`POJO Customization </fundamentals/data-formats/pojo-customization>`.
224+
the guide on :doc:`POJO Customization </fundamentals/data-formats/pojo-customization>`.
224225

225226
For more information about the methods and classes mentioned in this section,
226-
see the following API Documentation:
227+
see the following API documentation:
227228

228229
- `CodecRegistry <{+api+}/apidocs/bson/org/bson/codecs/configuration/CodecRegistry.html>`__
229230
- `TDocument <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html>`__
@@ -245,5 +246,4 @@ by performing the following:
245246
- Specify the **automatic** conversion behavior for the ``PojoCodecProvider``
246247
to apply the ``Codecs`` to any class and its properties.
247248
- Add the ``PojoCodecProvider`` to a ``CodecRegistry``, and specify the
248-
registry on an instance of a ``MongoClient``, ``MongoDatabase``, or
249-
``MongoCollection``.
249+
registry on an instance of a ``MongoDatabase`` or ``MongoCollection``.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
.. _fundamentals-records:
2+
3+
=============================
4+
Document Data Format: Records
5+
=============================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to store and retrieve data in the {+driver-long+}
19+
using **Java records**. Java records are a type of Java class often used to
20+
model data and separate business logic from data representation.
21+
22+
.. tip::
23+
24+
You can declare Java records in Java 16 or later. Learn more about the
25+
functionality and restrictions of records from `Java 17 Language Updates: Record Classes <https://docs.oracle.com/en/java/javase/17/language/records.html>`__.
26+
27+
If you are using an earlier version of Java, you can use plain old Java
28+
objects instead. See the :ref:`<fundamentals-pojos>` guide for
29+
implementation details.
30+
31+
.. _fundamentals-example-record:
32+
33+
Serialize and Deserialize a Record
34+
----------------------------------
35+
36+
The driver natively supports encoding and decoding Java records for
37+
MongoDB read and write operations using the **default codec registry**. The
38+
default codec registry is a collection of classes called **codecs** that
39+
define how to convert encode and decode Java types. Learn more about
40+
codecs and the default codec registry in the guide on :ref:`<fundamentals-codecs>`.
41+
42+
Example Record
43+
~~~~~~~~~~~~~~
44+
45+
The code examples in this guide reference the following sample record, which
46+
describes a data storage device:
47+
48+
.. literalinclude:: /includes/fundamentals/code-snippets/records/DataStorageRecord.java
49+
:language: java
50+
:start-after: start dataStorageRecord
51+
:end-before: end dataStorageRecord
52+
53+
Insert a Record
54+
~~~~~~~~~~~~~~~
55+
56+
You can insert a ``DataStorageRecord`` instance as shown in the following code:
57+
58+
.. code-block:: java
59+
60+
MongoCollection<DataStorageRecord> collection = database.getCollection("data_storage_devices", DataStorageRecord.class);
61+
62+
// insert the record
63+
collection.insertOne(new DataStorageRecord("2TB SSD", 1.71));
64+
65+
Retrieve a Record
66+
~~~~~~~~~~~~~~~~~
67+
68+
You can retrieve documents as ``DataStorageRecord`` instances and print them
69+
as shown in the following code:
70+
71+
.. io-code-block::
72+
:copyable: true
73+
74+
.. input::
75+
:language: java
76+
77+
MongoCollection<DataStorageRecord> collection = database.getCollection("data_storage_devices", DataStorageRecord.class);
78+
79+
// retrieve and print the records
80+
List<DataStorageRecord> records = new ArrayList<DataStorageRecord>();
81+
collection.find().into(records);
82+
records.forEach(System.out::println);
83+
84+
.. output::
85+
:language: none
86+
87+
DataStorageRecord[productName=1TB SSD, capacity=1.71]
88+

source/fundamentals/data-formats/pojo-customization.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _fundamentals-pojo-customization:
2+
13
==================
24
POJO Customization
35
==================
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fundamentals.codecs.records;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.mongodb.client.MongoClient;
7+
import com.mongodb.client.MongoClients;
8+
import com.mongodb.client.MongoCollection;
9+
import com.mongodb.client.MongoDatabase;
10+
11+
12+
public class CodecRecordExample {
13+
public static void main(String args[]) {
14+
// Replace the uri string with your MongoDB deployment's connection string
15+
String uri = "<connection uri>";
16+
try (MongoClient mongoClient = MongoClients.create(uri)) {
17+
18+
MongoDatabase database = mongoClient.getDatabase("sample_data");
19+
MongoCollection<DataStorageRecord> collection = database.getCollection("data_storage_devices", DataStorageRecord.class);
20+
21+
// insert the document
22+
collection.insertOne(new DataStorageRecord("2TB SSD", 1.71));
23+
24+
// return all documents in the collection as records
25+
List<DataStorageRecord> records = new ArrayList<DataStorageRecord>();
26+
collection.find().into(records);
27+
records.forEach(System.out::println);
28+
}
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package fundamentals.codecs.records;
2+
3+
// start dataStorageRecord
4+
public record DataStorageRecord(
5+
String productName,
6+
double capacity
7+
) {}
8+
// end dataStorageRecord

source/whats-new.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Client-Side Field Level Encryption and Queryable Encryption.
3232
The bug can cause data corruption when rotating :ref:`Data Encryption Keys <csfle-key-architecture>`
3333
(DEKs) encrypted with a :ref:`Customer Master Key <csfle-key-architecture>`
3434
hosted on Google Cloud Key Management Service or Azure
35-
Key Vault. The bug was present in version 4.7.0 of the driver
35+
Key Vault. The bug was present in version 4.7.0 of the driver
3636
in the ``RewrapManyDataKey`` method and causes the
3737
loss of your DEKs.
3838

@@ -117,7 +117,8 @@ New features of the 4.6 Java driver release include:
117117
and the `DnsClientProvider <https://mongodb.github.io/mongo-java-driver/4.6/apidocs/mongodb-driver-core/com/mongodb/spi/dns/DnsClientProvider.html>`__
118118
interface API documentation for more information.
119119
- Added driver support for encoding and decoding between `Java records <https://docs.oracle.com/en/java/javase/17/language/records.html>`__
120-
and BSON documents, which is enabled by default.
120+
and BSON documents, which is enabled by default. See :ref:`<fundamentals-records>`
121+
for more information.
121122

122123
.. _version-4.5:
123124

0 commit comments

Comments
 (0)