You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While most workloads have a predictable set of fields used in query filters and predicates, adhoc query patterns may use filters on any field in the json document structure.
19
+
Workloads which have an unpredictable set of fields in the schema can use wildcard indexes to support queries against arbitrary or unknown fields, for optimized performance.
20
20
21
21
Wildcard indexing can be helpful in the following scenarios:
22
22
- Queries filtering on any field in the document making indexing all fields through a single command easier than indexing each field individually.
23
23
- Queries filtering on most fields in the document making indexing all but a few fields through a single easier than indexing most fields individually.
24
24
25
+
## Indexing all fields
26
+
27
+
Set up a wildcard index to facilitate queries on all possible document fields, including those with unknown or dynamic names.
28
+
29
+
```javascript
30
+
db.collection.createIndex( { "$**":1 } )
31
+
```
32
+
33
+
> [!IMPORTANT]
34
+
> For large collections, we recommend using alternate approach defined later in this doc.
35
+
36
+
## Include or exclude specific fields
37
+
38
+
Wildcard indexes can also be restricted to specific fields while excluding certain fields from being targeted for indexing. Let's review a sample for the following Json.
39
+
40
+
```json
41
+
{
42
+
"firstName": "Steve",
43
+
"lastName": "Smith",
44
+
"companyName": "Microsoft",
45
+
"division": "Azure",
46
+
"timeInOrgInYears": 7
47
+
}
48
+
```
49
+
50
+
We can control the indexing behavior, the example restricts creating indexes on `firstName`,`lastName` & `timeInOrgInYears` field.
51
+
52
+
```javascript
53
+
db.collection.createIndex( { "$**":1 },
54
+
{"wildcardProjection": { "firstName":0
55
+
, "lastName":0
56
+
, "companyName":1
57
+
, "division":1
58
+
, "timeInOrgInYears":0
59
+
}
60
+
}
61
+
)
62
+
```
63
+
64
+
In the wildcardProjection document, the value 0 or 1 indicates whether the field is included (1) or excluded (0) from indexing.
65
+
66
+
## Alternative for indexing all fields
67
+
25
68
This sample describes a simple workaround to minimize the effort needed to create individual indexes until wildcard indexing is generally available in Azure Cosmos DB for MongoDB vCore.
26
69
27
-
## Solution
28
70
Consider the json document below:
29
71
```json
30
72
{
@@ -70,9 +112,9 @@ While this sample document only requires a combination of 10 fields to be explic
70
112
71
113
The jar file detailed in the rest of this document makes indexing fields in larger documents simpler. The jar takes a sample JSON document as input, parses the document and executes createIndex commands for each field without the need for user intervention.
72
114
73
-
## Prerequisites
115
+
###Prerequisites
74
116
75
-
### Java 21
117
+
####Java 21
76
118
After the virtual machine is deployed, use SSH to connect to the machine, and install CQLSH using the below commands:
77
119
78
120
```bash
@@ -81,7 +123,7 @@ sudo apt update
81
123
sudo apt install openjdk-21-jdk
82
124
```
83
125
84
-
## Sample jar to create individual indexes for all fields
126
+
###Sample jar to create individual indexes for all fields
85
127
86
128
Clone the repository containing the Java sample to iterate through each field in the JSON document's structure and issue createIndex operations for each field in the document.
87
129
@@ -99,7 +141,7 @@ The cloned repository does not need to be built if there are no changes to be ma
The jar file is designed to not wait on a response from each createIndex operation. The indexes are created asynchronously on the server and the progress of the index build operation on the cluster can be tracked.
104
146
105
147
Consider this sample to track indexing progress on the 'cosmicworks' database.
@@ -143,8 +185,8 @@ When a createIndex operation is in progress, the response looks like:
143
185
}
144
186
```
145
187
146
-
## Related content
147
-
148
-
Check out the full sample here - https://github.com/Azure-Samples/cosmosdb-mongodb-vcore-wildcard-indexing
188
+
## Next Steps
149
189
150
-
Check out [indexing best practices](how-to-create-indexes.md), which details best practices for indexing on Azure Cosmos DB for MongoDB vCore.
190
+
- Refer for code sample - https://github.com/Azure-Samples/cosmosdb-mongodb-vcore-wildcard-indexing
191
+
- Review here for [Indexing and Limitations](indexing.md)
192
+
- Learn about [Indexing best practices](how-to-create-indexes.md)
0 commit comments