boolean-type
- boolean-type
- How to Index Boolean Fields
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean Type
- Define the Index for the boolean Type
- Configure boolean Field Properties
- Try an Example for the boolean 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 Boolean Fields
Use the MongoDB Search boolean field type to index true and false values. After you index a field as boolean, you can query it 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, and sort results by the indexed field.
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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
- 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 boolean 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": "<collectionName>",
"database": "<databaseName>",
"name": "default",
"type": "search",
"definition": {
"mappings": {
"dynamic": true | false,
"fields": {
"<fieldName>": {
"type": "boolean",
}
}
}
}
}'
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
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": "customers",
"database": "sample_analytics",
"name": "default",
"type": "search",
"definition": {
"mappings": {
"dynamic": true,
"fields": {
"active": {
"type": "boolean",
}
}
}
}
}'
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- Create a
search-index.jsonconfiguration file and specify thebooleantype.
json
{
"mappings": {
"dynamic": true | false,
"fields": {
"<fieldName>": {
"type": "boolean"
}
}
}
}
- 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.
- Create a MongoDB Search index.
Run the atlas clusters search indexes create command in your terminal, replacing <path-to-file> with the path to the search-index.json file:
shell
atlas clusters search indexes create --clusterName <cluster-name> --file <path-to-file>
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
{
"mappings": {
"dynamic": false,
"fields": {
"active": {
"type": "boolean"
}
}
}
}
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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
- Specify the database and collection.
On the Database screen, click the name of the database, then click the name of the collection.
- 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": {
"<fieldName>": {
"type": "boolean"
}
}
}
}
Click Create Search Index.
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
{
"mappings": {
"dynamic": false,
"fields": {
"active": {
"type": "boolean"
}
}
}
}
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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.
- 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>
- Run the
db.collection.createSearchIndex()method to create the index.
shell
db.<collection>.createSearchIndex(
"default",
{
"mappings": {
"dynamic": true|false,
"fields": {
"<field-name>": {
"type": "boolean",
}
}
}
}
)
Output:
text
default
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
// Connect to your Atlas cluster
use sample_analytics;
// Create the search index
db.movies.createSearchIndex(
"default",
{
"mappings": {
"dynamic": false,
"fields": {
"active": {
"type": "boolean"
}
}
}
});
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
Choose your preferred configuration method in the Atlas UI and then select the database and collection.
Visual Editor
-
Click Refine Your Index to configure your index.
-
In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.
-
Click Customized Configuration.
-
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.
-
Click the Data Type dropdown and select Boolean.
-
Configure the field properties for the
booleantype. 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. -
Click Add.
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
Visual Editor
-
In the Add Field Mapping window, select address.location from the Field Name dropdown.
-
Click the Data Type dropdown and select Boolean.
-
Click Add.
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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.
- 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
int main (void) { mongoc_client_t client = NULL; mongoc_collection_t collection = NULL; mongoc_database_t *database = NULL; bson_error_t error; bson_t cmd = BSON_INITIALIZER; bool ok = true;
mongoc_init();
// Connect to your Atlas deployment
client = mongoc_client_new("<connectionString>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
ok = false;
goto cleanup;
}
// Access your database and collection
database = mongoc_client_get_database(client, "<databaseName>");
collection = mongoc_database_get_collection(database, "<collectionName>");
// Specify the command and the new index
const char *cmd_str = BSON_STR({
"createSearchIndexes" : "<collectionName>",
"indexes" : [ {
"name" : "default",
"definition" : {
"mappings": {
"dynamic": true|false,
"fields": {
"<fieldName>" : {
"type": "boolean",
}
}
},
}
} ]
});
// Convert your command to BSON
if (!bson_init_from_json(&cmd, cmd_str, -1, &error)) {
fprintf(stderr, "Failed to parse command: %s\n", error.message);
ok = false;
goto cleanup;
}
// Create the MongoDB Search index by running the command
if (!mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
fprintf(stderr, "Failed to run createSearchIndexes: %s\n", error.message);
ok = false;
goto cleanup;
}
printf ("Index created!\n");
cleanup: mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_database_destroy (database); bson_destroy (&cmd); mongoc_cleanup (); return ok ? EXIT_SUCCESS : EXIT_FAILURE; }
```
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
- 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
```
The preceding code performs the following actions:
-
Configures a C project.
-
Creates a
index.outexecutable for your application. -
Finds and requires the C driver. Replace the
<version>placeholder with your C driver version number, such as2.0.0. -
Links the program to the
libmongoclibrary.
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.
- 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 boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
#include <mongoc/mongoc.h>
#include <stdlib.h>
int main (void)
{
mongoc_client_t *client = NULL;
mongoc_collection_t *collection = NULL;
mongoc_database_t *database = NULL;
bson_error_t error;
bson_t cmd = BSON_INITIALIZER;
bool ok = true;
mongoc_init();
// Connect to your Atlas deployment
client = mongoc_client_new("<connectionString>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
ok = false;
goto cleanup;
}
// Access your database and collection
database = mongoc_client_get_database(client, "sample_analytics");
collection = mongoc_database_get_collection(database, "customers");
// Specify the command and the new index
const char *cmd_str = BSON_STR({
"createSearchIndexes" : "customers",
"indexes" : [ {
"name" : "default",
"definition" : {
"mappings": {
"dynamic": true,
"fields": {
"active" : {
"type": "boolean",
}
}
},
}
} ]
});
// Convert your command to BSON
if (!bson_init_from_json(&cmd, cmd_str, -1, &error)) {
fprintf(stderr, "Failed to parse command: %s\n", error.message);
ok = false;
goto cleanup;
}
// Create the MongoDB Search index by running the command
if (!mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
fprintf(stderr, "Failed to run createSearchIndexes: %s\n", error.message);
ok = false;
goto cleanup;
}
printf ("Index created!\n");
cleanup:
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
mongoc_database_destroy (database);
bson_destroy (&cmd);
mongoc_cleanup ();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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.
- 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 = "<connection-string>";
var client = new MongoClient(uri);
var db = client.GetDatabase("<database-name>");
var collection = db.GetCollection<BsonDocument>("<collection-name>");
// Create the MongoDB Search index definition for the boolean field
var index = new CreateSearchIndexModel(
"default", new BsonDocument
{
{ "mappings", new BsonDocument
{
{ "dynamic", true | false },
{ "fields", new BsonDocument
{
{ "<fieldName>", new BsonDocument
{
{ "type", "boolean" },
}
}
}
}
}
}
});
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
- Create the index.
shell
dotnet run Program.cs
Output:
text
New index name: default
Configure boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
using MongoDB.Bson;
using MongoDB.Driver;
// Replace the connection string with your MongoDB deployment's connection string
var uri = "<connection-string>";
var client = new MongoClient(uri);
var db = client.GetDatabase("sample_analytics");
var collection = db.GetCollection<BsonDocument>("customers");
// Create the MongoDB Search index definition for the boolean field
var index = new CreateSearchIndexModel(
"default", new BsonDocument
{
{ "mappings", new BsonDocument
{
{ "dynamic", false },
{ "fields", new BsonDocument
{
{ "active", new BsonDocument
{
{ "type", "boolean" },
}
}
}
}
}
}
});
var result = collection.SearchIndexes.CreateOne(index);
Console.WriteLine($"New index name: {result}");
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by indexed boolean fields.
You can also use the boolean type to index:
-
Fields whose value is an array of booleans. To learn more, see [How to Index the Elements of an Array.](https://www.mongodb.com/docs/search/indexes/field-types/array-type.md#std-label-fts-array-ref
-
Boolean fields inside an array of documents indexed as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.
To facet on boolean values, use [MongoDB View](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-documents-collections to convert the value to string type. For a demonstration of transforming an boolean type to string type for faceting, see [Transform Fields for Faceting.](https://www.mongodb.com/docs/search/about/view-support.md#std-label-fts-transform-for-facet
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.
Define the Index for the boolean Type
- 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.
- 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
using bsoncxx::builder::basic::kvp; using bsoncxx::builder::basic::make_document;
int main(){
mongocxx::instance instance;
mongocxx::uri uri("<connection-string>");
mongocxx::client client(uri);
auto db = client["<databaseName>"];
auto collection = db["<collectionName>"];
// instantiate a ``mongocxx::search_index_view`` on your collection
auto siv = collection.search_indexes();
{
auto name = "default";
auto definition = make_document(
kvp("mappings", make_document(
kvp("dynamic", true|false),
kvp("fields", make_document(
kvp("<fieldName>", make_document(
kvp("type", "boolean")
))
))
))
);
auto model = mongocxx::search_index_model(name, definition.view());
// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << 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
- 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 boolean Field Properties
The MongoDB Search boolean type takes the following parameter:
| Option | Type | Necessity | Description |
|---|---|---|---|
type |
string | required | Human-readable label that identifies this field type. Value must be boolean. |
Try an Example for the boolean Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the boolean type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
The following example creates a MongoDB Search index on the sample_analytics.customers collection and indexes the active field as the boolean type:
#include <iostream>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
int main(){
mongocxx::instance instance;
mongocxx::uri uri("<connection-string>");
mongocxx::client client(uri);
auto db = client["sample_analytics"];
auto collection = db["customers"];
// instantiate a ``mongocxx::search_index_view`` on your collection
auto siv = collection.search_indexes();
{
auto name = "default";
auto definition = make_document(
kvp("mappings", make_document(
kvp("dynamic", false),
kvp("fields", make_document(
kvp("active", make_document(
kvp("type", "boolean")
))
))
))
);
auto model = mongocxx::search_index_model(name, definition.view());
// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
}
}
You can use the MongoDB Search boolean type to index true and false values. You can query fields of type boolean 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. You can also [sort](https://www.mongodb.com/docs/search/query/sort.md#std-label-sort-ref by
(本文超出站点单文档体积上限,已截断)
