Skip to content

DOCSP-49082 - Indexes (Comprehensive Coverage) #623

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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 35 additions & 83 deletions source/indexes.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _csharp-indexes:

=======
Indexes
=======
=========================
Create and Manage Indexes
=========================

.. facet::
:name: genre
Expand Down Expand Up @@ -70,7 +70,18 @@
-----------

MongoDB provides several different index types to support querying
your data. The following sections describe the most common index types
your data. The following steps describe the process for creating an index:

- Use the ``IndexKeysDefinitionBuilder<TDocument>`` class, accessible through the

Check failure on line 75 in source/indexes.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.AvoidAccessible] Use accessible only to refer to things that all people, including those with disabilities, can easily use. Don’t use it to mean simple or open. Raw Output: {"message": "[MongoDB.AvoidAccessible] Use accessible only to refer to things that all people, including those with disabilities, can easily use. Don’t use it to mean simple or open.", "location": {"path": "source/indexes.txt", "range": {"start": {"line": 75, "column": 60}}}, "severity": "ERROR"}
``Builders<TDocument>.IndexKeys`` property, to create one or more
``IndexKeysDefinition<TDocument>`` objects. These key definitions describe the type
of index to create and the index's other properties.
- Create a new ``CreateIndexModel<TDocument>`` object. Pass the key definitions from the
previous step to the constructor.
- Call the ``CreateOne()`` method on your collection's ``Indexes`` property. Pass
the ``CreateIndexModel<TDocument>`` object from the previous step.

The following sections describe the most common index types
and provide sample code for creating each index type.

.. note::
Expand Down Expand Up @@ -313,9 +324,15 @@
Geospatial Indexes
~~~~~~~~~~~~~~~~~~

MongoDB supports queries of geospatial coordinate data using **2dsphere
indexes**. With a 2dsphere index, you can query the geospatial data for
inclusion, intersection, and proximity.
You can query geospatial coordinate data in MongoDB by using **2d** or
**2dsphere indexes**.

2dsphere Indexes
++++++++++++++++

2dsphere indexes support geospatial queries on an earth-like sphere. By using a 2dsphere
index, you can query the geospatial data For inclusion, intersection, and proximity.
The indexed field must be either GeoJSON objects or legacy coordinate pairs.

To create a 2dsphere index, you must specify a field that contains
only **GeoJSON objects**. For more details about this type, see :manual:`GeoJSON objects </reference/geojson>`
Expand Down Expand Up @@ -367,7 +384,11 @@
:end-before: end-geospatial-query
:dedent:

MongoDB also supports ``2d`` indexes for calculating distances on a Euclidean plane and
2d Indexes
++++++++++

The {+driver-short+} also includes a ``Geo2D`` method for creating 2d indexes.
You can use these indexes to calculate distances on a Euclidean plane and
for working with the "legacy coordinate pairs" syntax used in MongoDB 2.2 and earlier.
To learn more, see :manual:`Geospatial Queries </geospatial-queries>` in the Server manual.

Expand Down Expand Up @@ -433,81 +454,12 @@
:end-before: end-list-indexes
:dedent:

.. TODO: integrate into existing page

Sample Class
------------

The code examples in this guide demonstrate how you can use builders to
create types to interact with documents in the sample collection ``plants.flowers``.
Documents in this collection are modeled by the following ``Flower`` class:

.. literalinclude:: /includes/fundamentals/code-examples/builders.cs
:language: csharp
:dedent:
:start-after: start-model
:end-before: end-model

Each builder class takes a generic type parameter
``TDocument`` which represents the type of document that you are working
with. In this guide, the ``Flower`` class is the document type used in
each builder class example.

.. _csharp-builders-indexes:

Define Index Keys
-----------------

The ``IndexKeysDefinitionBuilder`` class provides a type-safe interface for
defining index keys. Suppose you want to select ``Category`` as an
ascending index key.

Use builders to select the index key with the typed variant:

.. code-block:: csharp
:copyable: true

var builder = Builders<Flower>.IndexKeys;
var keys = builder.Ascending(f => f.Category);

Alternatively, you can use string-based field names to select the index key:

.. code-block:: csharp
:copyable: true

var builder = Builders<BsonDocument>.IndexKeys;
var keys = builder.Ascending("Category");

The ``IndexKeysDefinitionBuilder`` class also provides methods to build
a wildcard index. You can create a wildcard index using ``All field paths`` or ``A
single field path``, in this case using ``Category``:

.. tabs::

.. tab:: ``All field paths``
:tabid: all-wildcard-index

.. code-block:: csharp
:copyable: true

var builder = Builders<Flower>.IndexKeys;
var keys = builder.Wildcard();

.. tab:: ``A single field path``
:tabid: single-wildcard-index

.. code-block:: csharp
:copyable: true

var builder = Builders<Flower>.IndexKeys;

// Using the typed variant
var keys = builder.Wildcard(f => f.Category);

// Using string-based field names
var keys = builder.Wildcard("Category");
Additional Information
----------------------

For more information about how to use wildcard indexes, see
:manual:`Wildcard Indexes </core/indexes/index-types/index-wildcard>`.
For more information about the classes and methods used on this page, see the following
API documentation:

- `CreateOne() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoIndexManager-1.CreateOne.html>`__
- `CreateIndexModel<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.CreateIndexModel-1.html>`__
- `IndexKeysDefinitionBuilder <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IndexKeysDefinitionBuilder-1.html>`__
2 changes: 1 addition & 1 deletion source/reference/quick-reference.txt
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ their related reference and API documentation.
* - | **Create an Index**
|
| `API Documentation <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoIndexManager-1.CreateOne.html>`__
| :ref:`Fundamentals <csharp-builders-indexes>`
| :ref:`Fundamentals <csharp-indexes>`

- .. code-block:: csharp
:copyable: true
Expand Down
Loading