-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
117c49f
DOCSP-41161: Builders landing page
mcmorisi 5acaf2b
Fixes
mcmorisi 0162a25
Address RR feedback
mcmorisi 0e670a7
Trigger autobuild
mcmorisi d8e638c
Fix
mcmorisi 9d707dc
Further feedback
mcmorisi 0946436
Address technical feedback
mcmorisi bcd00a7
Address further technical feedback
mcmorisi 4c49729
Fix
mcmorisi 82f255f
Merge branch 'master' into DOCSP-41161-builders
mcmorisi 6c385f7
Fix
mcmorisi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,110 @@ | ||||||||||
.. _kotlin-sync-builders: | ||||||||||
|
||||||||||
================ | ||||||||||
Use Builders API | ||||||||||
================ | ||||||||||
|
||||||||||
.. 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 use the builders API and demonstrates the utility provided by | ||||||||||
builder classes. | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
The {+driver-short+} provides type-safe classes and methods that enable developers to | ||||||||||
efficiently build queries and aggregations. | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
Why Use Builders? | ||||||||||
----------------- | ||||||||||
|
||||||||||
When using the MongoDB shell or plain Kotlin to construct BSON query documents, you are not | ||||||||||
able to identify errors in your code until runtime. | ||||||||||
|
||||||||||
With builder classes, you can write operators as methods and any syntax errors will be | ||||||||||
caught at compile time. | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
Example | ||||||||||
------- | ||||||||||
|
||||||||||
This section describes three methods of fetching the ``email`` field values of documents | ||||||||||
in the ``users`` collection that meet the following criteria: | ||||||||||
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.
Suggested change
|
||||||||||
|
||||||||||
- Users that identify as ``female`` | ||||||||||
- Users that are older than ``29`` | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
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: | ||||||||||
|
||||||||||
Using the MongoDB Shell | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
The following sample executes the previously defined query using the MongoDB shell: | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
.. code-block:: js | ||||||||||
|
||||||||||
collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 }) | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
Without Using Builders | ||||||||||
~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
The following example executes the previously defined query using plain {+language+}: | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
.. literalinclude:: /includes/builders/builders.kt | ||||||||||
:start-after: start-find | ||||||||||
:end-before: end-find | ||||||||||
:language: kotlin | ||||||||||
:copyable: | ||||||||||
:dedent: | ||||||||||
|
||||||||||
Using Builders | ||||||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
The following example executes the previously defined query using the builders API: | ||||||||||
mcmorisi 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
|
||||||||||
.. - :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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
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, | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
val gender: String, | ||||||
val age: Int, | ||||||
val email: String | ||||||
) | ||||||
// end-user-class | ||||||
|
||||||
// start-result-class | ||||||
data class Result( | ||||||
val email: String | ||||||
) | ||||||
// end-result-class | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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<Result>(filter).projection(projection) | ||||||
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.
Suggested change
|
||||||
// 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<Result>(filter).projection(projection) | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// end-find-builders | ||||||
} | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.