0
# MongoDB API Resource Management
1
2
Full management of MongoDB API resources including databases, collections, role definitions, and user definitions. The MongoDB API provides compatibility with MongoDB drivers and tools while leveraging Cosmos DB's global distribution and enterprise features.
3
4
## Capabilities
5
6
### MongoDB Database Management
7
8
Create, configure, and manage MongoDB databases with throughput and scaling options.
9
10
```python { .api }
11
def list_mongo_db_databases(
12
self,
13
resource_group_name: str,
14
account_name: str
15
) -> ItemPaged[MongoDBDatabaseGetResults]:
16
"""
17
List MongoDB databases in a Cosmos DB account.
18
19
Parameters:
20
- resource_group_name: Name of the resource group
21
- account_name: Name of the Cosmos DB account
22
23
Returns:
24
ItemPaged[MongoDBDatabaseGetResults]: Paginated list of MongoDB databases
25
"""
26
27
def get_mongo_db_database(
28
self,
29
resource_group_name: str,
30
account_name: str,
31
database_name: str
32
) -> MongoDBDatabaseGetResults:
33
"""
34
Get properties of a MongoDB database.
35
36
Parameters:
37
- resource_group_name: Name of the resource group
38
- account_name: Name of the Cosmos DB account
39
- database_name: Name of the MongoDB database
40
41
Returns:
42
MongoDBDatabaseGetResults: MongoDB database properties and configuration
43
"""
44
45
def begin_create_update_mongo_db_database(
46
self,
47
resource_group_name: str,
48
account_name: str,
49
database_name: str,
50
create_update_mongo_db_database_parameters: MongoDBDatabaseCreateUpdateParameters
51
) -> LROPoller[MongoDBDatabaseGetResults]:
52
"""
53
Create or update a MongoDB database (Long Running Operation).
54
55
Parameters:
56
- resource_group_name: Name of the resource group
57
- account_name: Name of the Cosmos DB account
58
- database_name: Name of the MongoDB database to create/update
59
- create_update_mongo_db_database_parameters: Database configuration parameters
60
61
Returns:
62
LROPoller[MongoDBDatabaseGetResults]: Poller for monitoring operation progress
63
"""
64
65
def begin_delete_mongo_db_database(
66
self,
67
resource_group_name: str,
68
account_name: str,
69
database_name: str
70
) -> LROPoller[None]:
71
"""
72
Delete a MongoDB database (Long Running Operation).
73
74
Parameters:
75
- resource_group_name: Name of the resource group
76
- account_name: Name of the Cosmos DB account
77
- database_name: Name of the MongoDB database to delete
78
79
Returns:
80
LROPoller[None]: Poller for monitoring operation progress
81
"""
82
```
83
84
### MongoDB Collection Management
85
86
Manage MongoDB collections with sharding, indexing, and MongoDB-specific features.
87
88
```python { .api }
89
def list_mongo_db_collections(
90
self,
91
resource_group_name: str,
92
account_name: str,
93
database_name: str
94
) -> ItemPaged[MongoDBCollectionGetResults]:
95
"""
96
List MongoDB collections in a database.
97
98
Parameters:
99
- resource_group_name: Name of the resource group
100
- account_name: Name of the Cosmos DB account
101
- database_name: Name of the MongoDB database
102
103
Returns:
104
ItemPaged[MongoDBCollectionGetResults]: Paginated list of MongoDB collections
105
"""
106
107
def get_mongo_db_collection(
108
self,
109
resource_group_name: str,
110
account_name: str,
111
database_name: str,
112
collection_name: str
113
) -> MongoDBCollectionGetResults:
114
"""
115
Get properties of a MongoDB collection.
116
117
Parameters:
118
- resource_group_name: Name of the resource group
119
- account_name: Name of the Cosmos DB account
120
- database_name: Name of the MongoDB database
121
- collection_name: Name of the MongoDB collection
122
123
Returns:
124
MongoDBCollectionGetResults: MongoDB collection properties and configuration
125
"""
126
127
def begin_create_update_mongo_db_collection(
128
self,
129
resource_group_name: str,
130
account_name: str,
131
database_name: str,
132
collection_name: str,
133
create_update_mongo_db_collection_parameters: MongoDBCollectionCreateUpdateParameters
134
) -> LROPoller[MongoDBCollectionGetResults]:
135
"""
136
Create or update a MongoDB collection (Long Running Operation).
137
138
Parameters:
139
- resource_group_name: Name of the resource group
140
- account_name: Name of the Cosmos DB account
141
- database_name: Name of the MongoDB database
142
- collection_name: Name of the MongoDB collection to create/update
143
- create_update_mongo_db_collection_parameters: Collection configuration parameters
144
145
Returns:
146
LROPoller[MongoDBCollectionGetResults]: Poller for monitoring operation progress
147
"""
148
149
def begin_delete_mongo_db_collection(
150
self,
151
resource_group_name: str,
152
account_name: str,
153
database_name: str,
154
collection_name: str
155
) -> LROPoller[None]:
156
"""
157
Delete a MongoDB collection (Long Running Operation).
158
159
Parameters:
160
- resource_group_name: Name of the resource group
161
- account_name: Name of the Cosmos DB account
162
- database_name: Name of the MongoDB database
163
- collection_name: Name of the MongoDB collection to delete
164
165
Returns:
166
LROPoller[None]: Poller for monitoring operation progress
167
"""
168
```
169
170
### MongoDB Throughput Management
171
172
Manage throughput settings for databases and collections with autoscale and manual options.
173
174
```python { .api }
175
def get_mongo_db_database_throughput(
176
self,
177
resource_group_name: str,
178
account_name: str,
179
database_name: str
180
) -> ThroughputSettingsGetResults:
181
"""
182
Get throughput settings for a MongoDB database.
183
184
Parameters:
185
- resource_group_name: Name of the resource group
186
- account_name: Name of the Cosmos DB account
187
- database_name: Name of the MongoDB database
188
189
Returns:
190
ThroughputSettingsGetResults: Current throughput configuration
191
"""
192
193
def begin_update_mongo_db_database_throughput(
194
self,
195
resource_group_name: str,
196
account_name: str,
197
database_name: str,
198
update_throughput_parameters: ThroughputSettingsUpdateParameters
199
) -> LROPoller[ThroughputSettingsGetResults]:
200
"""
201
Update throughput settings for a MongoDB database (Long Running Operation).
202
203
Parameters:
204
- resource_group_name: Name of the resource group
205
- account_name: Name of the Cosmos DB account
206
- database_name: Name of the MongoDB database
207
- update_throughput_parameters: New throughput configuration
208
209
Returns:
210
LROPoller[ThroughputSettingsGetResults]: Poller for monitoring operation progress
211
"""
212
213
def get_mongo_db_collection_throughput(
214
self,
215
resource_group_name: str,
216
account_name: str,
217
database_name: str,
218
collection_name: str
219
) -> ThroughputSettingsGetResults:
220
"""
221
Get throughput settings for a MongoDB collection.
222
223
Parameters:
224
- resource_group_name: Name of the resource group
225
- account_name: Name of the Cosmos DB account
226
- database_name: Name of the MongoDB database
227
- collection_name: Name of the MongoDB collection
228
229
Returns:
230
ThroughputSettingsGetResults: Current throughput configuration
231
"""
232
233
def begin_migrate_mongo_db_database_to_autoscale(
234
self,
235
resource_group_name: str,
236
account_name: str,
237
database_name: str
238
) -> LROPoller[ThroughputSettingsGetResults]:
239
"""
240
Migrate MongoDB database from manual to autoscale throughput (Long Running Operation).
241
242
Parameters:
243
- resource_group_name: Name of the resource group
244
- account_name: Name of the Cosmos DB account
245
- database_name: Name of the MongoDB database
246
247
Returns:
248
LROPoller[ThroughputSettingsGetResults]: Poller for monitoring operation progress
249
"""
250
251
def begin_migrate_mongo_db_collection_to_manual_throughput(
252
self,
253
resource_group_name: str,
254
account_name: str,
255
database_name: str,
256
collection_name: str
257
) -> LROPoller[ThroughputSettingsGetResults]:
258
"""
259
Migrate MongoDB collection from autoscale to manual throughput (Long Running Operation).
260
261
Parameters:
262
- resource_group_name: Name of the resource group
263
- account_name: Name of the Cosmos DB account
264
- database_name: Name of the MongoDB database
265
- collection_name: Name of the MongoDB collection
266
267
Returns:
268
LROPoller[ThroughputSettingsGetResults]: Poller for monitoring operation progress
269
"""
270
```
271
272
### MongoDB Role Management
273
274
Manage custom role definitions for MongoDB API security and access control.
275
276
```python { .api }
277
def list_mongo_db_role_definitions(
278
self,
279
resource_group_name: str,
280
account_name: str
281
) -> ItemPaged[MongoRoleDefinitionGetResults]:
282
"""
283
List MongoDB role definitions for access control.
284
285
Parameters:
286
- resource_group_name: Name of the resource group
287
- account_name: Name of the Cosmos DB account
288
289
Returns:
290
ItemPaged[MongoRoleDefinitionGetResults]: Paginated list of role definitions
291
"""
292
293
def get_mongo_db_role_definition(
294
self,
295
resource_group_name: str,
296
account_name: str,
297
mongo_role_definition_id: str
298
) -> MongoRoleDefinitionGetResults:
299
"""
300
Get properties of a MongoDB role definition.
301
302
Parameters:
303
- resource_group_name: Name of the resource group
304
- account_name: Name of the Cosmos DB account
305
- mongo_role_definition_id: Unique identifier of the role definition
306
307
Returns:
308
MongoRoleDefinitionGetResults: Role definition properties and permissions
309
"""
310
311
def begin_create_update_mongo_db_role_definition(
312
self,
313
resource_group_name: str,
314
account_name: str,
315
mongo_role_definition_id: str,
316
create_update_mongo_role_definition_parameters: MongoRoleDefinitionCreateUpdateParameters
317
) -> LROPoller[MongoRoleDefinitionGetResults]:
318
"""
319
Create or update a MongoDB role definition (Long Running Operation).
320
321
Parameters:
322
- resource_group_name: Name of the resource group
323
- account_name: Name of the Cosmos DB account
324
- mongo_role_definition_id: Unique identifier of the role definition
325
- create_update_mongo_role_definition_parameters: Role definition configuration
326
327
Returns:
328
LROPoller[MongoRoleDefinitionGetResults]: Poller for monitoring operation progress
329
"""
330
331
def begin_delete_mongo_db_role_definition(
332
self,
333
resource_group_name: str,
334
account_name: str,
335
mongo_role_definition_id: str
336
) -> LROPoller[None]:
337
"""
338
Delete a MongoDB role definition (Long Running Operation).
339
340
Parameters:
341
- resource_group_name: Name of the resource group
342
- account_name: Name of the Cosmos DB account
343
- mongo_role_definition_id: Unique identifier of the role definition
344
345
Returns:
346
LROPoller[None]: Poller for monitoring operation progress
347
"""
348
```
349
350
### MongoDB User Management
351
352
Manage user definitions with role assignments for MongoDB API authentication.
353
354
```python { .api }
355
def list_mongo_db_user_definitions(
356
self,
357
resource_group_name: str,
358
account_name: str
359
) -> ItemPaged[MongoUserDefinitionGetResults]:
360
"""
361
List MongoDB user definitions.
362
363
Parameters:
364
- resource_group_name: Name of the resource group
365
- account_name: Name of the Cosmos DB account
366
367
Returns:
368
ItemPaged[MongoUserDefinitionGetResults]: Paginated list of user definitions
369
"""
370
371
def get_mongo_db_user_definition(
372
self,
373
resource_group_name: str,
374
account_name: str,
375
mongo_user_definition_id: str
376
) -> MongoUserDefinitionGetResults:
377
"""
378
Get properties of a MongoDB user definition.
379
380
Parameters:
381
- resource_group_name: Name of the resource group
382
- account_name: Name of the Cosmos DB account
383
- mongo_user_definition_id: Unique identifier of the user definition
384
385
Returns:
386
MongoUserDefinitionGetResults: User definition properties and roles
387
"""
388
389
def begin_create_update_mongo_db_user_definition(
390
self,
391
resource_group_name: str,
392
account_name: str,
393
mongo_user_definition_id: str,
394
create_update_mongo_user_definition_parameters: MongoUserDefinitionCreateUpdateParameters
395
) -> LROPoller[MongoUserDefinitionGetResults]:
396
"""
397
Create or update a MongoDB user definition (Long Running Operation).
398
399
Parameters:
400
- resource_group_name: Name of the resource group
401
- account_name: Name of the Cosmos DB account
402
- mongo_user_definition_id: Unique identifier of the user definition
403
- create_update_mongo_user_definition_parameters: User definition configuration
404
405
Returns:
406
LROPoller[MongoUserDefinitionGetResults]: Poller for monitoring operation progress
407
"""
408
409
def begin_delete_mongo_db_user_definition(
410
self,
411
resource_group_name: str,
412
account_name: str,
413
mongo_user_definition_id: str
414
) -> LROPoller[None]:
415
"""
416
Delete a MongoDB user definition (Long Running Operation).
417
418
Parameters:
419
- resource_group_name: Name of the resource group
420
- account_name: Name of the Cosmos DB account
421
- mongo_user_definition_id: Unique identifier of the user definition
422
423
Returns:
424
LROPoller[None]: Poller for monitoring operation progress
425
"""
426
```
427
428
## Usage Examples
429
430
### Creating a MongoDB Database and Collection
431
432
```python
433
from azure.mgmt.cosmosdb import CosmosDBManagementClient
434
from azure.mgmt.cosmosdb.models import (
435
MongoDBDatabaseCreateUpdateParameters,
436
MongoDBDatabaseResource,
437
MongoDBCollectionCreateUpdateParameters,
438
MongoDBCollectionResource,
439
MongoIndex,
440
MongoIndexKeys,
441
CreateUpdateOptions
442
)
443
from azure.identity import DefaultAzureCredential
444
445
client = CosmosDBManagementClient(DefaultAzureCredential(), "subscription-id")
446
447
# Create MongoDB database with shared throughput
448
database_params = MongoDBDatabaseCreateUpdateParameters(
449
resource=MongoDBDatabaseResource(id="inventory"),
450
options=CreateUpdateOptions(throughput=800)
451
)
452
453
poller = client.mongo_db_resources.begin_create_update_mongo_db_database(
454
"my-resource-group",
455
"my-cosmos-account",
456
"inventory",
457
database_params
458
)
459
database = poller.result()
460
461
# Create MongoDB collection with sharding and indexes
462
collection_resource = MongoDBCollectionResource(
463
id="products",
464
shard_key={"category": "Hash"}, # Shard key for partitioning
465
indexes=[
466
MongoIndex(
467
key=MongoIndexKeys(keys=["name", "category"]),
468
options={"unique": False, "background": True}
469
),
470
MongoIndex(
471
key=MongoIndexKeys(keys=["price"]),
472
options={"name": "price_index"}
473
)
474
]
475
)
476
477
collection_params = MongoDBCollectionCreateUpdateParameters(
478
resource=collection_resource,
479
options=CreateUpdateOptions(throughput=400) # Dedicated throughput
480
)
481
482
poller = client.mongo_db_resources.begin_create_update_mongo_db_collection(
483
"my-resource-group",
484
"my-cosmos-account",
485
"inventory",
486
"products",
487
collection_params
488
)
489
collection = poller.result()
490
491
print(f"Created collection: {collection.name} with shard key: {collection.resource.shard_key}")
492
```
493
494
### Managing MongoDB Roles and Users
495
496
```python
497
from azure.mgmt.cosmosdb.models import (
498
MongoRoleDefinitionCreateUpdateParameters,
499
MongoRoleDefinitionResource,
500
Role,
501
Privilege,
502
MongoUserDefinitionCreateUpdateParameters,
503
MongoUserDefinitionResource
504
)
505
506
# Create custom role definition
507
role_resource = MongoRoleDefinitionResource(
508
role_name="ProductManager",
509
type="CustomRole",
510
database_name="inventory",
511
roles=[], # Inherited roles
512
privileges=[
513
Privilege(
514
resource={"db": "inventory", "collection": "products"},
515
actions=["insert", "update", "remove", "find"]
516
)
517
]
518
)
519
520
role_params = MongoRoleDefinitionCreateUpdateParameters(resource=role_resource)
521
522
poller = client.mongo_db_resources.begin_create_update_mongo_db_role_definition(
523
"my-resource-group",
524
"my-cosmos-account",
525
"product-manager-role",
526
role_params
527
)
528
role_def = poller.result()
529
530
# Create user with role assignment
531
user_resource = MongoUserDefinitionResource(
532
user_name="product_admin",
533
password="secure_password_123",
534
database_name="inventory",
535
custom_data="Product management user",
536
roles=[
537
Role(
538
role="ProductManager",
539
db="inventory"
540
)
541
],
542
mechanisms="SCRAM-SHA-256"
543
)
544
545
user_params = MongoUserDefinitionCreateUpdateParameters(resource=user_resource)
546
547
poller = client.mongo_db_resources.begin_create_update_mongo_db_user_definition(
548
"my-resource-group",
549
"my-cosmos-account",
550
"product-admin-user",
551
user_params
552
)
553
user_def = poller.result()
554
555
print(f"Created user: {user_def.resource.user_name} with roles: {[r.role for r in user_def.resource.roles]}")
556
```
557
558
## Key Types
559
560
```python { .api }
561
class MongoDBDatabaseGetResults:
562
"""MongoDB database properties and configuration."""
563
id: str
564
name: str
565
type: str
566
resource: MongoDBDatabaseGetPropertiesResource
567
options: MongoDBDatabaseGetPropertiesOptions
568
569
class MongoDBDatabaseResource:
570
"""MongoDB database resource definition."""
571
id: str # Database name
572
573
class MongoDBCollectionGetResults:
574
"""MongoDB collection properties and configuration."""
575
id: str
576
name: str
577
type: str
578
resource: MongoDBCollectionGetPropertiesResource
579
options: MongoDBCollectionGetPropertiesOptions
580
581
class MongoDBCollectionResource:
582
"""MongoDB collection resource definition."""
583
id: str # Collection name
584
shard_key: Dict[str, str] # Shard key for partitioning (e.g., {"category": "Hash"})
585
indexes: List[MongoIndex] # Collection indexes
586
analytical_storage_ttl: int # TTL for analytical storage
587
588
class MongoIndex:
589
"""MongoDB index definition."""
590
key: MongoIndexKeys # Index key specification
591
options: Dict[str, Any] # Index options (unique, background, etc.)
592
593
class MongoIndexKeys:
594
"""MongoDB index key specification."""
595
keys: List[str] # Fields to index
596
597
class MongoRoleDefinitionGetResults:
598
"""MongoDB role definition for access control."""
599
id: str
600
name: str
601
type: str
602
resource: MongoRoleDefinitionResource
603
604
class MongoRoleDefinitionResource:
605
"""MongoDB role definition resource."""
606
role_name: str # Name of the role
607
type: str # "BuiltInRole" or "CustomRole"
608
database_name: str # Database where role is defined
609
roles: List[Role] # Inherited roles
610
privileges: List[Privilege] # Role privileges
611
612
class Privilege:
613
"""MongoDB role privilege definition."""
614
resource: Dict[str, str] # Resource specification (db, collection)
615
actions: List[str] # Allowed actions (insert, update, remove, find, etc.)
616
617
class MongoUserDefinitionGetResults:
618
"""MongoDB user definition properties."""
619
id: str
620
name: str
621
type: str
622
resource: MongoUserDefinitionResource
623
624
class MongoUserDefinitionResource:
625
"""MongoDB user definition resource."""
626
user_name: str # Username
627
password: str # User password
628
database_name: str # Authentication database
629
custom_data: str # Custom user data
630
roles: List[Role] # Assigned roles
631
mechanisms: str # Authentication mechanism ("SCRAM-SHA-1", "SCRAM-SHA-256")
632
633
class Role:
634
"""MongoDB role assignment."""
635
role: str # Role name
636
db: str # Database where role applies
637
```