-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41161: Builders landing page #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
117c49f
5acaf2b
0162a25
0e670a7
d8e638c
9d707dc
0946436
bcd00a7
4c49729
82f255f
6c385f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
.. _kotlin-sync-builders: | ||
|
||
=============================== | ||
Implement Builders Code Pattern | ||
=============================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. .. toctree:: | ||
|
||
.. /fundamentals/builders/aggregates | ||
.. /fundamentals/builders/filters | ||
.. /fundamentals/builders/indexes | ||
.. /fundamentals/builders/projections | ||
.. /fundamentals/builders/sort | ||
.. /fundamentals/builders/updates | ||
|
||
Overview | ||
-------- | ||
|
||
This page describes how to implement the builders pattern in your code and describes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This seems a little verbose. In short its about using builders to help build operations in a less verbose way than using Documents. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For further clarification, do you think the entire Overview section is too verbose? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add suggesions |
||
benefits of using builder classes. | ||
|
||
The {+driver-short+} provides type-safe builder classes and methods that enable developers to | ||
efficiently build queries and aggregations. To learn more about the builders pattern, | ||
see `Type-safe builders <https://kotlinlang.org/docs/type-safe-builders.html>`__ in the | ||
{+language+} documentation. | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Why Use the Builders Code Pattern? | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
---------------------------------- | ||
|
||
If you use only plain {+language+} to construct BSON query documents, you are not | ||
able to identify errors in your code until runtime. When you use builder classes, you can | ||
write operators as methods and any syntax errors will be caught at compile time. | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example | ||
------- | ||
|
||
This section provides three equivalent ways to fetch the ``email`` field values of documents | ||
in the ``users`` collection that meet the following criteria: | ||
|
||
- ``gender`` value is ``"female"`` | ||
- ``age`` value is greater than ``29`` | ||
|
||
The following data class models the documents in the ``users`` collection: | ||
|
||
.. literalinclude:: /includes/builders/builders.kt | ||
:start-after: start-user-class | ||
:end-before: end-user-class | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
The following data class models the results returned by our query: | ||
|
||
.. literalinclude:: /includes/builders/builders.kt | ||
:start-after: start-result-class | ||
:end-before: end-result-class | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
MongoDB Query API | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
The following sample performs the query by using the MongoDB Query API: | ||
|
||
.. code-block:: js | ||
|
||
collection.find( | ||
{ "gender": "female", "age" : { "$gt": 29 }}, | ||
{ "_id": 0, "email": 1 } | ||
) | ||
|
||
Document Class Filter | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following example performs the query by using the ``Document`` class to construct the | ||
query filter: | ||
|
||
.. literalinclude:: /includes/builders/builders.kt | ||
:start-after: start-find | ||
:end-before: end-find | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
Builders Pattern | ||
~~~~~~~~~~~~~~~~ | ||
|
||
The following example performs the query by using the builders pattern: | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. literalinclude:: /includes/builders/builders.kt | ||
:start-after: start-find-builders | ||
:end-before: end-find-builders | ||
:language: kotlin | ||
:copyable: | ||
:dedent: | ||
|
||
.. TODO: Uncomment as pages get built | ||
|
||
.. Available Builders | ||
.. ------------------ | ||
|
||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. The following pages describe how to implement the different classes of builders | ||
.. available in the {+driver-short+}. | ||
|
||
.. - :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines. | ||
.. - :ref:`Filters <filters-builders>` for building query filters. | ||
.. - :ref:`Indexes <indexes-builders>` for creating index keys. | ||
.. - :ref:`Projections <projections-builders>` for building projections. | ||
.. - :ref:`Sorts <sorts-builders>` for building sort criteria. | ||
.. - :ref:`Updates <updates-builders>` for building updates. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import com.mongodb.ConnectionString | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.Filters | ||
import com.mongodb.client.model.Projections | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
import org.bson.codecs.pojo.annotations.BsonId | ||
import org.bson.types.ObjectId | ||
|
||
// start-user-class | ||
data class User( | ||
@BsonId val id: ObjectId, | ||
val gender: String, | ||
val age: Int, | ||
val email: String | ||
) | ||
// end-user-class | ||
|
||
// start-result-class | ||
data class Email( | ||
val email: String | ||
) | ||
// end-result-class | ||
|
||
fun main() { | ||
val uri = "<connection string URI>" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_restaurants") | ||
val collection = database.getCollection<User>("restaurants") | ||
|
||
// start-find | ||
val filter = Document("gender", "female").append("age", Document("\$gt", 29)) | ||
val projection = Document("_id", 0).append("email", 1) | ||
val results = collection.find<Email>(filter).projection(projection) | ||
// end-find | ||
|
||
// start-find-builders | ||
val filter = Filters.and( | ||
Filters.eq(User::gender.name, "female"), | ||
Filters.gt(User::age.name, 29) | ||
) | ||
|
||
val projection = Projections.fields( | ||
Projections.excludeId(), | ||
Projections.include("email") | ||
) | ||
|
||
val results = collection.find<Email>(filter).projection(projection) | ||
// end-find-builders | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this title makes sense - users aren't implementing builders we are consuming them.