|
1 | 1 | .. _csharp-indexes: |
2 | 2 |
|
3 | | -======= |
4 | | -Indexes |
5 | | -======= |
| 3 | +========================= |
| 4 | +Create and Manage Indexes |
| 5 | +========================= |
6 | 6 |
|
7 | 7 | .. facet:: |
8 | 8 | :name: genre |
@@ -70,7 +70,18 @@ Index Types |
70 | 70 | ----------- |
71 | 71 |
|
72 | 72 | MongoDB provides several different index types to support querying |
73 | | -your data. The following sections describe the most common index types |
| 73 | +your data. The following steps describe the process for creating an index: |
| 74 | + |
| 75 | +1. Use the ``IndexKeysDefinitionBuilder<TDocument>`` class, which you can access through the |
| 76 | + ``Builders<TDocument>.IndexKeys`` property, to create one or more |
| 77 | + ``IndexKeysDefinition<TDocument>`` objects. These key definitions describe the type |
| 78 | + of index to create and the index's other properties. |
| 79 | +#. Create a new ``CreateIndexModel<TDocument>`` object. Pass the key definitions from the |
| 80 | + previous step to the constructor. |
| 81 | +#. Call the ``CreateOne()`` method on your collection's ``Indexes`` property. Pass |
| 82 | + the ``CreateIndexModel<TDocument>`` object from the previous step. |
| 83 | + |
| 84 | +The following sections describe the most common index types |
74 | 85 | and provide sample code for creating each index type. |
75 | 86 |
|
76 | 87 | .. note:: |
@@ -313,9 +324,15 @@ and :manual:`Text Indexes </core/index-text>` in the Server manual. |
313 | 324 | Geospatial Indexes |
314 | 325 | ~~~~~~~~~~~~~~~~~~ |
315 | 326 |
|
316 | | -MongoDB supports queries of geospatial coordinate data using **2dsphere |
317 | | -indexes**. With a 2dsphere index, you can query the geospatial data for |
318 | | -inclusion, intersection, and proximity. |
| 327 | +You can query geospatial coordinate data in MongoDB by using **2d** or |
| 328 | +**2dsphere indexes**. |
| 329 | + |
| 330 | +2dsphere Indexes |
| 331 | +++++++++++++++++ |
| 332 | + |
| 333 | +2dsphere indexes support geospatial queries on an earth-like sphere. By using a 2dsphere |
| 334 | +index, you can query the geospatial data For inclusion, intersection, and proximity. |
| 335 | +The indexed field must be either GeoJSON objects or legacy coordinate pairs. |
319 | 336 |
|
320 | 337 | To create a 2dsphere index, you must specify a field that contains |
321 | 338 | only **GeoJSON objects**. For more details about this type, see :manual:`GeoJSON objects </reference/geojson>` |
@@ -367,7 +384,11 @@ The following is an example of a geospatial query using the "location.geo" index |
367 | 384 | :end-before: end-geospatial-query |
368 | 385 | :dedent: |
369 | 386 |
|
370 | | -MongoDB also supports ``2d`` indexes for calculating distances on a Euclidean plane and |
| 387 | +2d Indexes |
| 388 | +++++++++++ |
| 389 | + |
| 390 | +The {+driver-short+} also includes a ``Geo2D`` method for creating 2d indexes. |
| 391 | +You can use these indexes to calculate distances on a Euclidean plane and |
371 | 392 | for working with the "legacy coordinate pairs" syntax used in MongoDB 2.2 and earlier. |
372 | 393 | To learn more, see :manual:`Geospatial Queries </geospatial-queries>` in the Server manual. |
373 | 394 |
|
@@ -433,81 +454,12 @@ all indexes in a collection: |
433 | 454 | :end-before: end-list-indexes |
434 | 455 | :dedent: |
435 | 456 |
|
436 | | -.. TODO: integrate into existing page |
437 | | - |
438 | | -Sample Class |
439 | | ------------- |
440 | | - |
441 | | -The code examples in this guide demonstrate how you can use builders to |
442 | | -create types to interact with documents in the sample collection ``plants.flowers``. |
443 | | -Documents in this collection are modeled by the following ``Flower`` class: |
444 | | - |
445 | | -.. literalinclude:: /includes/fundamentals/code-examples/builders.cs |
446 | | - :language: csharp |
447 | | - :dedent: |
448 | | - :start-after: start-model |
449 | | - :end-before: end-model |
450 | | - |
451 | | -Each builder class takes a generic type parameter |
452 | | -``TDocument`` which represents the type of document that you are working |
453 | | -with. In this guide, the ``Flower`` class is the document type used in |
454 | | -each builder class example. |
455 | | - |
456 | | -.. _csharp-builders-indexes: |
457 | | - |
458 | | -Define Index Keys |
459 | | ------------------ |
460 | | - |
461 | | -The ``IndexKeysDefinitionBuilder`` class provides a type-safe interface for |
462 | | -defining index keys. Suppose you want to select ``Category`` as an |
463 | | -ascending index key. |
464 | | - |
465 | | -Use builders to select the index key with the typed variant: |
466 | | - |
467 | | -.. code-block:: csharp |
468 | | - :copyable: true |
469 | | - |
470 | | - var builder = Builders<Flower>.IndexKeys; |
471 | | - var keys = builder.Ascending(f => f.Category); |
472 | | - |
473 | | -Alternatively, you can use string-based field names to select the index key: |
474 | | - |
475 | | -.. code-block:: csharp |
476 | | - :copyable: true |
477 | | - |
478 | | - var builder = Builders<BsonDocument>.IndexKeys; |
479 | | - var keys = builder.Ascending("Category"); |
480 | | - |
481 | | -The ``IndexKeysDefinitionBuilder`` class also provides methods to build |
482 | | -a wildcard index. You can create a wildcard index using ``All field paths`` or ``A |
483 | | -single field path``, in this case using ``Category``: |
484 | | - |
485 | | -.. tabs:: |
486 | | - |
487 | | - .. tab:: ``All field paths`` |
488 | | - :tabid: all-wildcard-index |
489 | | - |
490 | | - .. code-block:: csharp |
491 | | - :copyable: true |
492 | | - |
493 | | - var builder = Builders<Flower>.IndexKeys; |
494 | | - var keys = builder.Wildcard(); |
495 | | - |
496 | | - .. tab:: ``A single field path`` |
497 | | - :tabid: single-wildcard-index |
498 | | - |
499 | | - .. code-block:: csharp |
500 | | - :copyable: true |
501 | | - |
502 | | - var builder = Builders<Flower>.IndexKeys; |
503 | | - |
504 | | - // Using the typed variant |
505 | | - var keys = builder.Wildcard(f => f.Category); |
506 | | - |
507 | | - // Using string-based field names |
508 | | - var keys = builder.Wildcard("Category"); |
| 457 | +Additional Information |
| 458 | +---------------------- |
509 | 459 |
|
510 | | -For more information about how to use wildcard indexes, see |
511 | | -:manual:`Wildcard Indexes </core/indexes/index-types/index-wildcard>`. |
| 460 | +For more information about the classes and methods used on this page, see the following |
| 461 | +API documentation: |
512 | 462 |
|
| 463 | +- `CreateOne() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoIndexManager-1.CreateOne.html>`__ |
| 464 | +- `CreateIndexModel<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.CreateIndexModel-1.html>`__ |
513 | 465 | - `IndexKeysDefinitionBuilder <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IndexKeysDefinitionBuilder-1.html>`__ |
0 commit comments