uuid-type

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: vib, jsonib, cloud, local, maven bom, gradle bom, maven, gradle -->

How to Index UUID Fields for Efficient Filtering and Sorting

Use the MongoDB Search uuid field type to index [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields. After you index a field as uuid, you can query and sort results by the indexed field using the [equals](https://www.mongodb.com/docs/search/query/operators-collectors/equals.md#std-label-equals-ref and [in](https://www.mongodb.com/docs/search/query/operators-collectors/in.md#std-label-in-ref operators.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Retrieve your Service Account access token for the project.

Use your service account to generate an access token, which authenticates your requests to the Atlas Administration API. To learn how to generate an access token, see [Make an API (Application Programming Interface) Request.](https://www.mongodb.com/docs/atlas/configure-api-access.md#std-label-example-api-request

  1. Make an API (Application Programming Interface) call.

Replace the following values in the curl command below. This command sends a POST request to your cluster to create a MongoDB Search index with the uuid field type.

  • Replace {ACCESS-TOKEN} with the output from the preceding step

  • Replace {groupId} with the project ID of the project where you want to create the MongoDB Search index

  • Replace {clusterName} with the name of the cluster where you want to create the MongoDB Search index

```shell curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header "Accept: application/vnd.atlas.2025-03-12+json" \ --header "Content-Type: application/json" \ -X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ -d '{ "collectionName": "", "database": "", "name": "default", "type": "search", "definition": { "mappings": { "dynamic": true|false, "fields": { "": { "type": "uuid" } } } } }'

```

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Create a search-index.json configuration file and specify the uuid type.

```json { "name": "default", "collectionName": "", "database": "", "mappings": { "dynamic": true|false, "fields": { "": { "type": "uuid" } } } }

```

  1. Connect to the Atlas CLI

In your terminal, connect to your cluster from the Atlas CLI. To learn more, see Connect from the Atlas CLI.

  1. Create a MongoDB Search index.

Run the atlas clusters search indexes create command in your terminal, replacing <clusterName> with the name of the cluster, and <path-to-file> with the path to the search-index.json file:

```shell atlas clusters search indexes create --clusterName --file

```

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Connect to your cluster using MongoDB Compass.

Open Compass and connect to your cluster. For detailed instructions, see [Connect to a Cluster via Compass.](https://www.mongodb.com/docs/atlas/compass-connection.md#std-label-atlas-connect-via-compass

  1. Specify the database and collection.

On the Database screen, click the name of the database, then click the name of the collection.

  1. Create the MongoDB Search index.

Click the Indexes tab, then select Search Indexes.

Click Create Atlas Search Index to open the index creation dialog box.

Specify a name for the index.

Your MongoDB Search index is named default by default. If you keep this name, then your index will be the default Search index for any MongoDB Search query that does not specify a different index option in its [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-fts-operators. If you are creating multiple indexes, we recommend that you maintain a consistent, descriptive naming convention across your indexes.

Specify the JSON (Javascript Object Notation) MongoDB Search index definition.

```json { "mappings": { "dynamic": true|false, "fields": { "": { "type": "uuid" } } } }

```

Click Create Search Index.

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Connect to the deployment by using mongosh.

In your terminal, connect to your Atlas cloud-hosted deployment or local deployment from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh. For detailed instructions on how to connect, see Connect to a Deployment.

  1. Switch to the database that contains the collection for which you want to create the index.

shell use <database>

Output:

shell switched to db <database>

  1. Run the db.collection.createSearchIndex() method to create the index.

shell db.<collection>.createSearchIndex( "default", { "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "uuid" } } } } )

Output:

text default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

Choose your preferred configuration method in the Atlas UI and then select the database and collection.

Visual Editor

  1. Click Refine Your Index to configure your index.

  2. In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.

  3. Click Customized Configuration.

  4. Select the field to index from the Field Name dropdown.

Note:

You can't index fields that contain the dollar ($) sign at the start of the field name.

  1. Click the Data Type dropdown and select UUID.

  2. Configure the field properties for the uuid type. To learn more, see [Field Properties](https://www.mongodb.com/docs/search/indexes/field-types/uuid-type.md#std-label-fts-field-types-uuid-options.

  3. Click Add.

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up the C project.

In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project for this project:

shell # Create a new directory for the project mkdir atlas-search-project && cd atlas-search-project

Add the C driver to your project by following the instructions in the MongoDB C Driver documentation.

  1. Define the index.

Create a create_index.c file in your project directory, and copy and paste the following code into the file.

```c #include #include

int main(int argc, char argv[]) { mongoc_client_t client; mongoc_collection_t collection; bson_error_t error; bson_t cmd; bson_t index_definition; bool success;

   /* Initialize the MongoDB C Driver. */
   mongoc_init();

   /* Connect to your MongoDB deployment. */
   client = mongoc_client_new("<connection-string>");
   collection = mongoc_client_get_collection (client, "<database>", "<collection>");

   if (!client) {
       fprintf(stderr, "Failed to create client.\n");
       return 1;
   }

   /* Create the MongoDB Search index definition for the UUID field. */
   char *cmd_str = bson_strdup_printf (
       BSON_STR ({
           "createSearchIndexes": "%s",
           "indexes": [{
               "name": "default",
               "definition": {
                   "mappings": {
                       "dynamic": true|false,
                       "fields": {
                           "<field-name>": {
                               "type": "uuid"
                           }
                       }
                   }
               }
           }]
       }),
       "<collection>"
   );
   bson_init_from_json (&cmd, cmd_str, -1, &error);
   bson_free (cmd_str);

   if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
       printf ("Index created!\n");
   } else {
       fprintf (stderr, "Failed to create search index: %s", error.message);
   }

   /* Clean up. */
   bson_destroy (&cmd);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy(client);
   mongoc_cleanup();

   return 0;

} ```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Set up a CMake application

To configure your application, create a CMakeLists.txt file in your project directory. Then, add the following code to the file:

```txt cmake_minimum_required(VERSION 3.11) project(atlas-search-index LANGUAGES C) add_executable (index.out create_index.c) find_package(mongoc REQUIRED) target_link_libraries(index.out mongoc::mongoc)

```

The preceding code performs the following actions:

  • Configures a C project.

  • Creates a index.out executable for your application.

  • Finds and requires the C driver. Replace the <version> placeholder with your C driver version, such as 2.0.0.

  • Links the program to the libmongoc library.

Note:

In the sample CMakeLists.txt file, the mongoc::mongoc target points to either the static library or the shared library. The library type depends on which one is available and whichever type the user specifies in the MONGOC_DEFAULT_IMPORTED_LIBRARY_TYPE CMake configuration setting. If you don't set this value and both library types are available, mongoc::mongoc uses the static library.

You can use the mongoc::static target to explicitly use the static library or the mongoc::shared target to use the shared library.

  1. Create the index.

In your terminal, run the following commands to build and run this application:

shell cmake -S. -Bcmake-build cmake --build cmake-build --target index.out ./cmake-build/index.out

Output:

text Index created!

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up and initialize the .NET/C# project.

In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project and initialize your project in that directory:

```shell # Create a new directory and initialize the project mkdir atlas-search-project && cd atlas-search-project dotnet new console

# Add the MongoDB .NET/C# Driver to your project dotnet add package MongoDB.Driver ```

For more detailed installation instructions, see the MongoDB .NET/C# Driver documentation.

  1. Define the index.

Paste the following code into the Program.cs file.

```csharp using MongoDB.Bson; using MongoDB.Driver;

// Replace the connection string with your MongoDB deployment's connection string var uri = "";

var client = new MongoClient(uri);

var db = client.GetDatabase(""); var collection = db.GetCollection("");

// Create the MongoDB Search index definition for the UUID field var index = new CreateSearchIndexModel( "default", new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true|false }, { "fields", new BsonDocument { { "", new BsonDocument { { "type", "uuid" } } } } } } } });

var result = collection.SearchIndexes.CreateOne(index); Console.WriteLine($"New index name: {result}"); ```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Create the index.

shell dotnet run Program.cs

Output:

text New index name: default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up the C++ project.

In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project for this project:

shell # Create a new directory for the project mkdir atlas-search-project && cd atlas-search-project

For more detailed installation instructions, see the MongoDB C++ Driver documentation.

  1. Define the index.

Create a CreateIndex.cpp file in your project directory, and copy and paste the following code into the file.

```cpp #include

#include #include #include #include #include

using bsoncxx::builder::basic::kvp; using bsoncxx::builder::basic::make_document;

int main() { // Initialize the MongoDB C++ driver mongocxx::instance instance{};

   // Connect to your MongoDB deployment
   mongocxx::uri uri("<connection-string>");
   mongocxx::client client(uri);

   auto db = client["<database>"];
   auto collection = db["<collection>"];
   auto siv = collection.search_indexes();

   // Create the MongoDB Search index definition for the UUID field
   auto name = "default";
   auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true|false), kvp("fields", make_document(kvp("<field-name>", make_document(kvp("type", "uuid"))))))));
   auto model = mongocxx::search_index_model(name, definition.view());

   try {
       // Execute the command to create the index
       auto result = siv.create_one(model);
       std::cout << "New index name: " << result << std::endl;
   } catch (const std::exception& e) {
       std::cerr << "Error creating Atlas Search index: " << e.what() << std::endl;
   }

} ```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Create the index.

shell c++ --std=c++17 CreateIndex.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out

Output:

text New index name: default

Tip: MacOS Error

MacOS users might see the following error after running the preceding commands:

sh dyld[54430]: Library not loaded: @rpath/libmongocxx._noabi.dylib

To resolve this error, use the -Wl,``-rpath`` linker option to set the @rpath, as shown in the following code:

sh c++ --std=c++17 CreateIndex.cpp -Wl,-rpath,/usr/local/lib/ $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up and initialize the Go module.

In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project and initialize your project in that directory:

```shell # Create a new directory and initialize the project mkdir atlas-search-project && cd atlas-search-project go mod init atlas-search-project

# Add the MongoDB Go Driver to your project go get go.mongodb.org/mongo-driver/v2/mongo ```

For more detailed installation instructions, see the [MongoDB Go Driver documentation.](https://www.mongodb.com/docs/drivers/go/current/get-started.md#std-label-go-get-started

  1. Define the index.

Create a create_index.go file in your project directory, and copy and paste the following code into the file.

```go package main

import ( "context" "fmt" "log"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"

)

func main() { ctx := context.Background() uri := "" client, err := mongo.Connect(options.Client(). ApplyURI(uri)) if err != nil { panic(err) }

defer func() {
    if err := client.Disconnect(context.TODO()); err != nil {
        panic(err)
    }
}()

coll := client.Database("<database>").Collection("<collection>")
const indexName = "default"
opts := options.SearchIndexes().SetName(indexName).SetType("search")

// Define the MongoDB Search index for the UUID field
searchIndexModel := mongo.SearchIndexModel{
    Definition: bson.D{
        {"mappings", bson.D{
            {"dynamic", true | false},
            {"fields", bson.D{
                {"<field-name>", bson.D{
                    {"type", "uuid"},
                }},
            }},
        }},
    },
    Options: opts,
}

// Create the index
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
if err != nil {
    log.Fatalf("Failed to create the Atlas Search index: %v", err)
}

fmt.Println("Name of index created: " + searchIndexName)

}

```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

### Atlas Cluster

Your connection string should use the following format:

text mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net

IMPORTANT: Ensure that your connection string includes your database user's credentials. To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

  1. Create the index.

shell go run create_index.go

Output:

text New index name: default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up your Java project with the MongoDB Java driver.

In your IDE, create a new Maven or Gradle project. Add the Bill of Materials (BOM) for MongoDB JVM artifacts to your project to organize dependency versions. The BOM simplifies dependency management by ensuring that you maintain consistent and compatible versions of dependencies, such as between the Java driver and the core driver library. Use the BOM to avoid version conflicts and simplify upgrades.

Select from the following Maven and Gradle tabs to view instructions for adding the BOM for each dependency manager:

### Maven

Add the following code to the dependencyManagement list in your pom.xml file:

xml <dependencyManagement> <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-bom</artifactId> <version>5.5.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

To view a list of dependencies that the BOM manages, see the mongodb-driver-bom dependency listing on the Maven Repository website.

After adding the BOM, select from the following Maven and Gradle tabs to view instructions for adding the MongoDB Java Sync Driver as a dependency in your project:

### Maven

Add the following code to the dependencies array in your Maven project's pom.xml file:

```xml org.mongodb mongodb-driver-sync 4.11.1

```

Run your package manager to install the dependencies to your project.

For more detailed installation instructions and version compatibility, see the MongoDB Java Driver documentation.

  1. Define the index.

In your project's base package directory, create a CreateIndex.java file and copy and paste the following code into this file.

```java import com.mongodb.client.*; import org.bson.Document;

public class CreateIndex { public static void main(String[] args) { // Replace the connection string with your MongoDB deployment's connection string String connectionString = "";

       try (MongoClient mongoClient = MongoClients.create(connectionString)) {
           MongoDatabase database = mongoClient.getDatabase("<database>");
           MongoCollection<Document> collection = database.getCollection("<collection>");
           String indexName = "default";

           // Create the MongoDB Search index definition for the UUID field
           Document searchIdx = new Document(
                   "mappings",
                   new Document("dynamic", true|false)
                           .append("fields",
                                   new Document("<field-name>",
                                           new Document("type", "uuid")))
           );
           collection.createSearchIndex(indexName, searchIdx);
           System.out.println("New index name: " + indexName);
       } catch (Exception e) {
           System.err.println("Error creating Atlas Search index: " + e.getMessage());
       }
   }

}

```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Compile and run the file to create the index.

Compile and run your application in your IDE or your shell.

shell javac CreateIndex.java java CreateIndex

Output:

text New index name: default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Initialize your Node.js project.

```shell # Create a new directory and initialize the project mkdir atlas-search-project && cd atlas-search-project npm init -y

# Add the MongoDB Node.js Driver to your project npm install mongodb ```

For detailed installation instructions, see the MongoDB Node Driver documentation.

  1. Define the index.

Create a create-index.js file in your project directory, and copy and paste the following code into the file.

```javascript const { MongoClient } = require("mongodb");

// Replace the connection string with your MongoDB deployment's connection string const uri = "";

const client = new MongoClient(uri);

async function run() { try { const database = client.db(""); const collection = database.collection("");

   // Create the MongoDB Search index definition for the UUID field
   const index = {
       name: "default",
       definition: {
           "mappings": {
               "dynamic": true|false,
               "fields": {
                   "<field-name>": {
                       "type": "uuid"
                   }
               }
           }
       }
   }

   // Create the index
   const result = await collection.createSearchIndex(index);
   console.log("New index name: " + result);
 } finally {
   await client.close();
 }

}

run().catch(console.dir); ```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Create the index.

shell node create-index.js

Output:

text New index name: default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up the Python project.

```shell # Create a new directory for the project mkdir atlas-search-project && cd atlas-search-project

# Install PyMongo pip install pymongo ```

For detailed installation instructions, see [MongoDB Python Driver (PyMongo).](https://www.mongodb.com/docs/languages/python/pymongo-driver/get-started.md#std-label-pymongo-get-started-download-and-install

  1. Define the index.

Create a create_index.py file in your project directory, and copy and paste the following code into the file.

```python from pymongo import MongoClient from pymongo.operations import SearchIndexModel

# Connect to your Atlas deployment uri = "" client = MongoClient(uri)

database = client[""] collection = database[""]

# Create the MongoDB Search index definition for the UUID field search_index_model = SearchIndexModel( definition={ "mappings": { "dynamic": true|false, "fields": { "":{ "type": "uuid" } } }, }, name="default", )

# Create the index result = collection.create_search_index(model=search_index_model) print(f"New index name: {result}")

```

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

To learn more about finding your connection string, see [Connect to a Cluster via Client Libraries.](https://www.mongodb.com/docs/atlas/driver-connection.md#std-label-connect-via-driver

Note:

For a local or self-managed deployment, your connection string should use the following format:

text mongodb://localhost:<port-number>/?directConnection=true

To learn more, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri

  1. Create the index.

shell python create_index.py

Output:

text New index name: default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Create a search-index.json configuration file and specify the uuid type.

```json { "name": "default", "collectionName": "", "database": "", "mappings": { "dynamic": true|false, "fields": { "": { "type": "uuid" } } } }

```

  1. Connect to the Atlas CLI

In your terminal, connect to your local deployment from the Atlas CLI. To learn more, see Connect from the Atlas CLI.

  1. Create a MongoDB Search index.

Run the atlas deployments search indexes create command in your terminal, replacing <path-to-file> with the path to the search-index.json file:

```shell atlas deployments search indexes create --file

```

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Connect to your cluster using MongoDB Compass.

Open Compass and connect to your cluster. For detailed instructions, see [Connect to a Cluster via Compass.](https://www.mongodb.com/docs/atlas/compass-connection.md#std-label-atlas-connect-via-compass

  1. Specify the database and collection.

On the Database screen, click the name of the database, then click the name of the collection.

  1. Create the MongoDB Search index.

Click the Indexes tab, then select Search Indexes.

Click Create Atlas Search Index to open the index creation dialog box.

Specify a name for the index.

Your MongoDB Search index is named default by default. If you keep this name, then your index will be the default Search index for any MongoDB Search query that does not specify a different index option in its [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-fts-operators. If you are creating multiple indexes, we recommend that you maintain a consistent, descriptive naming convention across your indexes.

Specify the JSON (Javascript Object Notation) MongoDB Search index definition.

```json { "mappings": { "dynamic": true|false, "fields": { "": { "type": "uuid" } } } }

```

Click Create Search Index.

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Connect to the deployment by using mongosh.

In your terminal, connect to your Atlas cloud-hosted deployment or local deployment from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh. For detailed instructions on how to connect, see Connect to a Deployment.

  1. Switch to the database that contains the collection for which you want to create the index.

shell use <database>

Output:

shell switched to db <database>

  1. Run the db.collection.createSearchIndex() method to create the index.

shell db.<collection>.createSearchIndex( "default", { "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "uuid" } } } } )

Output:

text default

Configure uuid Field Properties

The MongoDB Search uuid type takes the following parameters:

Option Type Necessity Description
type uuid Required Human-readable label that identifies this field type. Value must be uuid.

Use Universally Unique Identifiers (UUIDs) when you need values that are virtually guaranteed to be unique. You can use the MongoDB Search uuid type to index, query, and [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref over [BSON Binary Subtype 4](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-binary-data fields.

Define the Index for the uuid Type

  1. Set up the C project.

In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project for this project:

shell # Create a new directory for the project mkdir atlas-search-project && cd atlas-search-project

Add the C driver to your project by following the instructions in the MongoDB C Driver documentation.

  1. Define the index.

Create a create_index.c file in your project directory, and copy and paste the following code into the file.

```c #include <mongoc/mongoc.h> #include <stdio.h>

int main(int argc, char *argv[]) {

(本文超出站点单文档体积上限,已截断)