Skip to content

Commit 626cf65

Browse files
authored
DOCSP-42607: v5.2 changes (mongodb#48)
* DOCSP-42607: v5.2 changes * NR PR fixes 1 * indentation fix
1 parent 814e917 commit 626cf65

File tree

7 files changed

+196
-36
lines changed

7 files changed

+196
-36
lines changed

.github/workflows/check-autobuilder.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ java-api = "https://mongodb.github.io/mongo-java-driver/{+version-number+}"
3434
core-api = "{+java-api+}/apidocs/mongodb-driver-core"
3535
kotlin-docs = "https://kotlinlang.org"
3636
serialization-version = "1.5.1"
37+
kotlinx-dt-version = "0.6.1"

source/data-formats/serialization.txt

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,98 @@ deserializes them accordingly.
297297

298298
Retrieving as Document type
299299
Document{{_id=..., _t=Teacher, name=Vivian Lee, department=History}}
300-
Document{{_id=..., _t=Student, name=Kate Parker, grade=10}}
300+
Document{{_id=..., _t=Student, name=Kate Parker, grade=10}}
301+
302+
.. _kotlin-sync-datetime-serialization:
303+
304+
Serialize Dates and Times
305+
-------------------------
306+
307+
In this section, you can learn about using {+language+} serialization to
308+
work with date and time types.
309+
310+
kotlinx-datetime Library
311+
~~~~~~~~~~~~~~~~~~~~~~~~
312+
313+
``kotlinx-datetime`` is a {+language+} library that offers
314+
a high level of control over how your date and time values
315+
are serialized. To use the library, add the ``kotlinx-datetime``
316+
dependency to your project's dependency list.
317+
318+
Select from the following tabs to see how to add the ``kotlinx-datetime``
319+
dependency to your project by using the :guilabel:`Gradle` and
320+
:guilabel:`Maven` package managers:
321+
322+
.. tabs::
323+
324+
.. tab::
325+
:tabid: Gradle
326+
327+
.. code-block:: kotlin
328+
:caption: build.gradle.kts
329+
330+
implementation("org.jetbrains.kotlinx:kotlinx-datetime:{+kotlinx-dt-version+}")
331+
332+
.. tab::
333+
:tabid: Maven
334+
335+
.. code-block:: kotlin
336+
:caption: pom.xml
337+
338+
<dependency>
339+
<groupId>org.jetbrains.kotlinx</groupId>
340+
<artifactId>kotlinx-datetime-jvm</artifactId>
341+
<version>{+kotlinx-dt-version+}</version>
342+
</dependency>
343+
344+
To learn more about this library, see the :github:`kotlinx-datetime repository
345+
</Kotlin/kotlinx-datetime>` on GitHub.
346+
347+
Example Data Class with Dates and Times
348+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349+
350+
After you add the library dependency, you can implement serializers from
351+
the ``kotlinx-datetime`` library that map your data class field values
352+
to the expected types in BSON.
353+
354+
In this example, the driver serializes the fields of
355+
the ``Appointment`` data class with the following behavior:
356+
357+
- ``name``: The driver serializes the value as a string.
358+
359+
- ``date``: The driver uses the ``kotlinx-datetime`` serializer
360+
because the field has the ``@Contextual`` annotation. ``LocalDate``
361+
values are serialized as BSON dates.
362+
363+
- ``time``: The driver serializes the value as a string because it does
364+
not have the ``@Contextual`` annotation. This is the default
365+
serialization behavior for ``LocalTime`` values.
366+
367+
.. literalinclude:: /includes/data-formats/serialization.kt
368+
:language: kotlin
369+
:start-after: start-datetime-data-class
370+
:end-before: end-datetime-data-class
371+
:dedent:
372+
373+
The following example inserts an instance of the ``Appointment`` data
374+
class into the ``appointments`` collection:
375+
376+
.. literalinclude:: /includes/data-formats/serialization.kt
377+
:language: kotlin
378+
:start-after: start-datetime-insertone
379+
:end-before: end-datetime-insertone
380+
:dedent:
381+
382+
In MongoDB, the ``LocalDate`` value is stored as a BSON date, and the
383+
``time`` field is stored as a string by default serialization:
384+
385+
.. code-block:: json
386+
387+
{
388+
"_id": ...,
389+
"name": "Daria Smith",
390+
"date": {
391+
"$date": "2024-10-15T00:00:00.000Z"
392+
},
393+
"time": "11:30",
394+
}

source/includes/data-formats/serialization.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ data class Teacher(
7474
) : Person
7575
// end-poly-classes
7676

77+
// start-datetime-data-class
78+
@Serializable
79+
data class Appointment(
80+
val name: String,
81+
@Contextual val date: LocalDate,
82+
val time: LocalTime,
83+
)
84+
// end-datetime-data-class
7785

7886
fun main() {
7987

@@ -126,5 +134,17 @@ fun main() {
126134
}
127135
// end-poly-operations
128136

137+
// start-datetime-insertone
138+
val collection = database.getCollection<Appointment>("appointments")
139+
140+
val apptDoc = Appointment(
141+
"Daria Smith",
142+
LocalDate(2024, 10, 15),
143+
LocalTime(hour = 11, minute = 30)
144+
)
145+
146+
collection.insertOne(apptDoc)
147+
// end-datetime-insertone
148+
129149
}
130150

source/includes/indexes/indexes.kt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,35 @@ fun main() {
7474

7575
// start-create-search-index
7676
val index = Document("mappings", Document("dynamic", true))
77-
collection.createSearchIndex("<index name>", index)
77+
collection.createSearchIndex("mySearchIdx", index)
7878
// end-create-search-index
7979

8080
// start-create-search-indexes
81-
val indexOne = SearchIndexModel("<first index name>", Document("mappings", Document("dynamic", true)))
82-
val indexTwo = SearchIndexModel("<second index name>", Document("mappings", Document("dynamic", true)))
83-
collection.createSearchIndexes(listOf(indexOne, indexTwo))
81+
val searchIdxMdl = SearchIndexModel(
82+
"searchIdx",
83+
Document("analyzer", "lucene.standard").append(
84+
"mappings", Document("dynamic", true)
85+
),
86+
SearchIndexType.search()
87+
)
88+
89+
val vectorSearchIdxMdl = SearchIndexModel(
90+
"vsIdx",
91+
Document(
92+
"fields",
93+
listOf(
94+
Document("type", "vector")
95+
.append("path", "embeddings")
96+
.append("numDimensions", 1536)
97+
.append("similarity", "dotProduct")
98+
)
99+
),
100+
SearchIndexType.vectorSearch()
101+
)
102+
103+
collection.createSearchIndexes(
104+
listOf(searchIdxMdl, vectorSearchIdxMdl)
105+
)
84106
// end-create-search-indexes
85107

86108
// start-list-search-indexes

source/indexes/atlas-search-index.txt

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
.. _kotlin-sync-atlas-search-index:
2+
.. _kotlin-sync-search-avs-indexes:
23

3-
====================
4-
Atlas Search Indexes
5-
====================
4+
======================================
5+
Atlas Search and Vector Search Indexes
6+
======================================
67

78
.. contents:: On this page
89
:local:
@@ -20,12 +21,23 @@ Atlas Search Indexes
2021
Overview
2122
--------
2223

23-
:atlas:`Atlas Search </atlas-search>` enables you to perform full-text searches on
24+
You can programmatically manage your :atlas:`Atlas Search
25+
</atlas-search>` and :atlas:`Atlas Vector Search
26+
</atlas-vector-search/vector-search-overview/>` indexes by using the
27+
{+driver-short+}.
28+
29+
Atlas Search enables you to perform full-text searches on
2430
collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of
2531
the search and which fields to index.
2632

27-
You can call the following methods on a collection to manage your Atlas Search
28-
indexes:
33+
Atlas Vector Search enables you to perform semantic searches on vector
34+
embeddings stored in MongoDB Atlas. Vector Search indexes define the
35+
indexes for the vector embeddings that you want to query and the boolean,
36+
date, objectId, numeric, string, or UUID values that you want to use to
37+
pre-filter your data.
38+
39+
You can call the following methods on a collection to manage your Atlas
40+
Search and Vector Search indexes:
2941

3042
- ``createSearchIndex()``
3143
- ``createSearchIndexes()``
@@ -35,9 +47,11 @@ indexes:
3547

3648
.. note::
3749

38-
The Atlas Search index management methods run asynchronously and might return before
39-
confirming that they ran successfully. To determine the current status of the indexes,
40-
call the ``listSearchIndexes()`` method.
50+
The Atlas Search and Vector Search index management methods run
51+
asynchronously and might return before confirming that they ran
52+
successfully. To determine the current status of the indexes, call
53+
the ``listSearchIndexes()`` method or view the indexes list in the
54+
:atlas:`Atlas UI </atlas-ui/indexes/>`.
4155

4256
The following sections provide code examples that demonstrate how to use
4357
each of the preceding methods.
@@ -50,13 +64,7 @@ Create a Search Index
5064
You can use the `createSearchIndex()
5165
<{+api+}/com.mongodb.kotlin.client/-mongo-collection/create-search-index.html>`__
5266
and the `createSearchIndexes() <{+api+}/com.mongodb.kotlin.client/-mongo-collection/create-search-indexes.html>`__
53-
methods to create one or more Atlas Search indexes.
54-
55-
You can also use these methods to create Atlas Vector Search Indexes.
56-
Atlas Vector Search enables you to perform semantic searches on vector
57-
embeddings stored in MongoDB Atlas. To learn more about this feature,
58-
see the :atlas:`Atlas Vector Search Overview
59-
</atlas-vector-search/vector-search-overview/>`.
67+
methods to create one or more Atlas Search or Vector Search indexes.
6068

6169
The following code example shows how to create an Atlas Search index:
6270

@@ -66,7 +74,8 @@ The following code example shows how to create an Atlas Search index:
6674
:end-before: end-create-search-index
6775
:dedent:
6876

69-
The following code example shows how to create multiple indexes:
77+
The following code example shows how to create Atlas Search and
78+
Vector Search indexes in one call:
7079

7180
.. literalinclude:: /includes/indexes/indexes.kt
7281
:language: kotlin
@@ -76,7 +85,7 @@ The following code example shows how to create multiple indexes:
7685

7786
To learn more about the syntax used to define Atlas Search indexes, see the
7887
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>` guide
79-
in the Atlas manual.
88+
in the Atlas documentation.
8089

8190
.. _kotlin-sync-atlas-search-index-list:
8291

source/whats-new.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,39 @@ What's New
1212

1313
Learn what's new in:
1414

15+
* :ref:`Version 5.2 <kotlin-sync-version-5.2>`
1516
* :ref:`Version 5.1.3 <kotlin-sync-version-5.1.3>`
1617
* :ref:`Version 5.1.2 <kotlin-sync-version-5.1.2>`
1718
* :ref:`Version 5.1.1 <kotlin-sync-version-5.1.1>`
1819
* :ref:`Version 5.1 <kotlin-sync-version-5.1>`
1920
* :ref:`Version 5.0 <kotlin-sync-version-5.0>`
2021

22+
.. _kotlin-sync-version-5.2:
23+
24+
What's New in 5.2
25+
-----------------
26+
27+
The 5.2 driver release includes the following new features,
28+
improvements, and fixes:
29+
30+
.. sharedinclude:: dbx/jvm/v5.2-wn-items.rst
31+
32+
.. replacement:: avs-index-link
33+
34+
the :ref:`kotlin-sync-search-avs-indexes` guide
35+
36+
- Adds support for serializers from the ``kotlinx-datetime`` library
37+
that let you map {+language+} date and time types to BSON as the
38+
expected types instead of as strings. To learn more, see the
39+
:ref:`kotlin-sync-datetime-serialization` section of the {+language+}
40+
Serialization guide.
41+
42+
- Supports serialization of `JsonElement
43+
<{+kotlin-docs+}/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-element/>`__
44+
values. To work with the ``JsonElement`` type, you must add the
45+
``kotlinx-serialization-json`` library as a dependency in your
46+
application.
47+
2148
.. _kotlin-sync-version-5.1.3:
2249

2350
What's New in 5.1.3

0 commit comments

Comments
 (0)