Skip to content

Commit 3897174

Browse files
authored
DOCSP-41161: Builders landing page (mongodb#28)
1 parent 7ee3261 commit 3897174

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ toc_landing_pages = [
1414
"/indexes",
1515
"work-with-indexes",
1616
"/data-formats",
17+
"/builders",
1718
"/aggregation"
1819
]
1920

source/builders.txt

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.. _kotlin-sync-builders:
2+
3+
=========================
4+
Use Builders Code Pattern
5+
=========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. .. toctree::
14+
15+
.. /fundamentals/builders/aggregates
16+
.. /fundamentals/builders/filters
17+
.. /fundamentals/builders/indexes
18+
.. /fundamentals/builders/projections
19+
.. /fundamentals/builders/sort
20+
.. /fundamentals/builders/updates
21+
22+
Overview
23+
--------
24+
25+
This page describes how to use the various available builders in your code and describes
26+
benefits of using the provided builders.
27+
28+
The {+driver-short+} provides type-safe builder classes and methods that enable developers to
29+
efficiently build queries and aggregations.
30+
31+
Why Use Builders?
32+
-----------------
33+
34+
If you use only plain {+language+} to construct BSON query documents, you are not
35+
able to identify syntax errors until runtime. The builders help ensure the corretness of
36+
syntax and can be less verbose than constructing BSON documents.
37+
38+
Example
39+
-------
40+
41+
This section provides three equivalent ways to fetch the ``email`` field values of documents
42+
in the ``users`` collection that meet the following criteria:
43+
44+
- ``gender`` value is ``"female"``
45+
- ``age`` value is greater than ``29``
46+
47+
The following data class models the documents in the ``users`` collection:
48+
49+
.. literalinclude:: /includes/builders/builders.kt
50+
:start-after: start-user-class
51+
:end-before: end-user-class
52+
:language: kotlin
53+
:copyable:
54+
:dedent:
55+
56+
The following data class models the results returned by our query:
57+
58+
.. literalinclude:: /includes/builders/builders.kt
59+
:start-after: start-result-class
60+
:end-before: end-result-class
61+
:language: kotlin
62+
:copyable:
63+
:dedent:
64+
65+
MongoDB Query API
66+
~~~~~~~~~~~~~~~~~
67+
68+
The following sample performs the query by using the MongoDB Query API:
69+
70+
.. code-block:: js
71+
72+
collection.find(
73+
{ "gender": "female", "age" : { "$gt": 29 }},
74+
{ "_id": 0, "email": 1 }
75+
)
76+
77+
Document Class Filter
78+
~~~~~~~~~~~~~~~~~~~~~
79+
80+
The following example performs the query by using the ``Document`` class to construct the
81+
query filter:
82+
83+
.. literalinclude:: /includes/builders/builders.kt
84+
:start-after: start-find
85+
:end-before: end-find
86+
:language: kotlin
87+
:copyable:
88+
:dedent:
89+
90+
Builders
91+
~~~~~~~~
92+
93+
The following example performs the query by using the builder helpers:
94+
95+
.. literalinclude:: /includes/builders/builders.kt
96+
:start-after: start-find-builders
97+
:end-before: end-find-builders
98+
:language: kotlin
99+
:copyable:
100+
:dedent:
101+
102+
.. TODO: Uncomment as pages get built
103+
104+
.. Available Builders
105+
.. ------------------
106+
107+
.. The following pages describe how to implement the different classes of builders
108+
.. available in the {+driver-short+}.
109+
110+
.. - :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines.
111+
.. - :ref:`Filters <filters-builders>` for building query filters.
112+
.. - :ref:`Indexes <indexes-builders>` for creating index keys.
113+
.. - :ref:`Projections <projections-builders>` for building projections.
114+
.. - :ref:`Sorts <sorts-builders>` for building sort criteria.
115+
.. - :ref:`Updates <updates-builders>` for building updates.

source/includes/builders/builders.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import com.mongodb.ConnectionString
2+
import com.mongodb.MongoClientSettings
3+
import com.mongodb.client.model.Filters
4+
import com.mongodb.client.model.Projections
5+
import com.mongodb.kotlin.client.MongoClient
6+
import org.bson.Document
7+
import org.bson.codecs.pojo.annotations.BsonId
8+
import org.bson.types.ObjectId
9+
10+
// start-user-class
11+
data class User(
12+
@BsonId val id: ObjectId,
13+
val gender: String,
14+
val age: Int,
15+
val email: String
16+
)
17+
// end-user-class
18+
19+
// start-result-class
20+
data class Email(
21+
val email: String
22+
)
23+
// end-result-class
24+
25+
fun main() {
26+
val uri = "<connection string URI>"
27+
28+
val settings = MongoClientSettings.builder()
29+
.applyConnectionString(ConnectionString(uri))
30+
.retryWrites(true)
31+
.build()
32+
33+
val mongoClient = MongoClient.create(settings)
34+
val database = mongoClient.getDatabase("sample_restaurants")
35+
val collection = database.getCollection<User>("restaurants")
36+
37+
// start-find
38+
val filter = Document("gender", "female").append("age", Document("\$gt", 29))
39+
val projection = Document("_id", 0).append("email", 1)
40+
val results = collection.find<Email>(filter).projection(projection)
41+
// end-find
42+
43+
// start-find-builders
44+
val filter = Filters.and(
45+
Filters.eq(User::gender.name, "female"),
46+
Filters.gt(User::age.name, 29)
47+
)
48+
49+
val projection = Projections.fields(
50+
Projections.excludeId(),
51+
Projections.include("email")
52+
)
53+
54+
val results = collection.find<Email>(filter).projection(projection)
55+
// end-find-builders
56+
}
57+

source/index.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/aggregation
2222
Use Aggregation Expression Operations </agg-exp-ops>
2323
/data-formats
24+
/builders
2425
/faq
2526
/connection-troubleshooting
2627
/issues-and-help
@@ -96,6 +97,11 @@ Specialized Data Formats
9697
Learn how to work with specialized data formats and custom types in the
9798
:ref:`kotlin-sync-data-formats` section.
9899

100+
Use Builders API
101+
----------------
102+
103+
Learn how to work with the builder operation helpers in the :ref:`kotlin-sync-builders` section.
104+
99105
FAQ
100106
---
101107

0 commit comments

Comments
 (0)