@@ -15,9 +15,9 @@ Document Data Format: Records
15
15
Overview
16
16
--------
17
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.
18
+ In this guide, you can learn how to store and retrieve data in the
19
+ {+driver-long+} using **Java records**. Java records are a type of Java class
20
+ often used to model data and separate business logic from data representation.
21
21
22
22
.. tip::
23
23
@@ -28,21 +28,21 @@ model data and separate business logic from data representation.
28
28
objects instead. See the :ref:`<fundamentals-pojos>` guide for
29
29
implementation details.
30
30
31
- .. _fundamentals-example-record:
32
-
33
31
Serialize and Deserialize a Record
34
32
----------------------------------
35
33
36
34
The driver natively supports encoding and decoding Java records for
37
35
MongoDB read and write operations using the **default codec registry**. The
38
36
default codec registry is a collection of classes called **codecs** that
39
- define how to convert encode and decode Java types. Learn more about
37
+ define how to convert encode and decode Java types. Learn more about
40
38
codecs and the default codec registry in the guide on :ref:`<fundamentals-codecs>`.
41
39
40
+ .. _fundamentals-example-record:
41
+
42
42
Example Record
43
43
~~~~~~~~~~~~~~
44
44
45
- The code examples in this guide reference the following sample record, which
45
+ The code examples in this section reference the following sample record, which
46
46
describes a data storage device:
47
47
48
48
.. literalinclude:: /includes/fundamentals/code-snippets/records/DataStorageRecord.java
@@ -86,3 +86,100 @@ as shown in the following code:
86
86
87
87
DataStorageRecord[productName=1TB SSD, capacity=1.71]
88
88
89
+
90
+ .. _fundamentals-records-annotations:
91
+
92
+ Specify Component Conversion Using Annotations
93
+ ----------------------------------------------
94
+
95
+ This section describes the annotations you can use to configure the
96
+ serialization behavior of record components and provides an example to
97
+ demonstrate the annotation behavior.
98
+
99
+ .. tip::
100
+
101
+ The ``org.bson.codecs.records.annotations`` package is deprecated. Use the
102
+ equivalent ones from the `org.bson.codecs.pojo.annotations <{+api+}/apidocs/bson/org/bson/codecs/pojo/annotations/package-summary.html>`__
103
+ package instead.
104
+
105
+ You can use the following annotations on record components:
106
+
107
+ .. list-table::
108
+ :header-rows: 1
109
+ :stub-columns: 1
110
+ :widths: 10 10
111
+
112
+ * - Annotation Name
113
+ - Description
114
+
115
+ * - ``BsonId``
116
+ - Specifies the component to serialize as the _id property.
117
+
118
+ * - ``BsonProperty``
119
+ - Specifies a custom document field name when converting the record
120
+ component to BSON. Accepts the field name as the parameter.
121
+
122
+ * - ``BsonRepresentation``
123
+ - Specifies a BSON type to store when different from the record component
124
+ type. Accepts the BSON type as the parameter.
125
+
126
+ Example Annotated Record
127
+ ~~~~~~~~~~~~~~~~~~~~~~~~
128
+
129
+ The code examples in this section reference the following sample record, which
130
+ describes a network device:
131
+
132
+ .. literalinclude:: /includes/fundamentals/code-snippets/records/NetworkDeviceRecord.java
133
+ :language: java
134
+ :start-after: start networkDeviceRecord
135
+ :end-before: end networkDeviceRecord
136
+
137
+ Insert an Annotated Record
138
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
139
+
140
+ You can insert a ``DataStorageRecord`` instance as shown in the following code:
141
+
142
+ .. code-block:: java
143
+
144
+ MongoCollection<NetworkDeviceRecord> collection = database.getCollection("network_devices", NetworkDeviceRecord.class);
145
+
146
+ // insert the record
147
+ String deviceId = new ObjectId().toHexString();
148
+ collection.insertOne(new NetworkDeviceRecord(deviceId, "Enterprise Wi-fi", "router"));
149
+
150
+ The inserted document in MongoDB should resemble the following:
151
+
152
+ .. code-block:: json
153
+ :copyable: false
154
+
155
+ {
156
+ _id: ObjectId("fedc..."),
157
+ name: 'Enterprise Wi-fi',
158
+ type: 'router'
159
+ }
160
+
161
+
162
+ Retrieve an Annotated Record
163
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164
+
165
+ You can retrieve documents as ``NetworkDeviceRecord`` instances and print them
166
+ as shown in the following code:
167
+
168
+ .. io-code-block::
169
+ :copyable: true
170
+
171
+ .. input::
172
+ :language: java
173
+
174
+ MongoCollection<NetworkDeviceRecord> collection = database.getCollection("network_devices", NetworkDeviceRecord.class);
175
+
176
+ // return all documents in the collection as records
177
+ List<NetworkDeviceRecord> records = new ArrayList<NetworkDeviceRecord>();
178
+ collection.find().into(records);
179
+ records.forEach(System.out::println);
180
+
181
+ .. output::
182
+ :language: none
183
+
184
+ NetworkDeviceRecord[deviceId=fedc..., name=Enterprise Wi-fi, deviceType=router]
185
+
0 commit comments