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
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb/vcore/indexing-basics.md
+33-17Lines changed: 33 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ This example application stores articles as documents with the following structu
41
41
}
42
42
```
43
43
44
-
###Single field indexes
44
+
## Single field indexes
45
45
46
46
Single field indexes store information from a single field in a collection. The sort order of the single field index doesn't matter. _id field remains indexed by default.
47
47
@@ -54,31 +54,37 @@ MongoDB vcore supports creating index at following
54
54
The following command creates a single field index on the field `author` and the following command creates it on an embedded field `firstName`.
55
55
56
56
```javascript
57
-
db.coll.createIndex({"author":1})
57
+
use cosmicworks
58
+
59
+
db.products.createIndex({"author":1})
58
60
59
61
// indexing embedded property
60
-
db.coll.createIndex({"author.firstName":-1})
62
+
db.products.createIndex({"author.firstName":-1})
61
63
```
62
64
63
65
One query can use multiple single field indexes where available.
64
66
65
67
> [!NOTE]
66
68
> Azure Cosmos DB for MongoDB vcore allows creating maximum of 64 indexes on a collection.
67
69
68
-
###Compound indexes
70
+
## Compound indexes
69
71
70
72
Compound indexes are required if your query needs the ability to **query or sort** data from two or more fields in each document in a collection.
71
73
72
74
The following command creates a compound index on the fields `author` and `launchDate` in opposite sort order.
Compounded indexes on nested fields aren't supported by default due to limitations with arrays. If your nested field doesn't contain an array, the index works as intended. If your nested field contains an array (anywhere on the path), that value is ignored in the index.
@@ -112,12 +118,14 @@ Compound index provides support for
112
118
- Maximum of 32 columns within a compound index.
113
119
- Indexing nested properties.
114
120
115
-
###Partial indexes
121
+
## Partial indexes
116
122
117
123
Indexes that have an associated query filter that describes when to generate a term in the index.
> While you can define only one text index per collection, Azure Cosmos DB for MongoDB vCore allows you to create text indexes on combination of multiple fields to enable you to perform text searches across different fields in your documents.
144
152
145
-
####Configure text index options
153
+
### Configure text index options
146
154
147
155
Text indexes in Azure Cosmos DB for MongoDB vcore come with several options to customize their behavior. For example, you can specify the language for text analysis, set weights to prioritize certain fields, and configure case-insensitive searches. Here's an example of creating a text index with options:
148
156
149
157
- Create an index to support search on both the `title` and `content` fields with English language support. Also, assign higher weights to the `title` field to prioritize it in search results.
> When a client performs a text search query with the term "Cosmos DB," the score for each document in the collection will be calculated based on the presence and frequency of the term in both the "title" and "content" fields, with higher importance given to the "title" field due to its higher weight.
160
170
161
-
####Perform a text search using a text index
171
+
### Perform a text search using a text index
162
172
163
173
Once the text index is created, you can perform text searches using the "text" operator in your queries. The text operator takes a search string and matches it against the text index to find relevant documents.
164
174
165
175
- Perform a text search for the phrase `Cosmos DB`.
166
176
167
177
```javascript
178
+
use cosmicworks
179
+
168
180
db.products.find(
169
181
{ $text: { $search:"Cosmos DB" } }
170
182
)
@@ -173,6 +185,8 @@ db.products.find(
173
185
- Optionally, use the `$meta` projection operator along with the `textScore` field in a query to see the weight
174
186
175
187
```javascript
188
+
use cosmicworks
189
+
176
190
db.products.find(
177
191
{ $text: { $search:"Cosmos DB" } },
178
192
{ score: { $meta:"textScore" } }
@@ -187,16 +201,18 @@ db.products.find(
187
201
- Hint() isn't supported in combination with a query using $text expression.
188
202
- Text indexes can be relatively large, consuming significant storage space compared to other index types.
189
203
190
-
###WildCard indexes
204
+
## WildCard indexes
191
205
192
206
Index on single field, indexes all paths beneath the `field` , excluding other fields that are on the same level. For example, for the following sample document
Creating an index on { "pets.$**": 1 }, creates index on details & sub-document properties but doesn't create an index on “familyName”.
@@ -208,7 +224,7 @@ Creating an index on { "pets.$**": 1 }, creates index on details & sub-document
208
224
- A compound wildcard index can only have 1 wildcard term and 1 or more additional index terms.
209
225
`{ "pets.$**": 1, “familyName”: 1 }`
210
226
211
-
###Geospatial indexes
227
+
## Geospatial indexes
212
228
213
229
Geospatial indexes support queries on data stored as GeoJSON objects or legacy coordinate pairs. You can use geospatial indexes to improve performance for queries on geospatial data or to run certain geospatial queries.
214
230
@@ -217,7 +233,7 @@ Azure Cosmos DB for MongoDB vcore provides two types of geospatial indexes:
217
233
- 2dsphere Indexes, which support queries that interpret geometry on a sphere.
218
234
- 2d Indexes, which support queries that interpret geometry on a flat surface.
219
235
220
-
####2d indexes
236
+
### 2d indexes
221
237
222
238
2d indexes are supported only with legacy coordinate pair style of storing geospatial data.
`2dsphere` indexes support geospatial queries on an earth-like sphere. It can support both GeoJSON objects or legacy coordinate pairs. `2dSphere` indexes work with the GeoJSON style of storing data, if legacy points are encountered then these are converted to GeoJSON point.
0 commit comments