Skip to content

Commit 094bcd3

Browse files
committed
Put in rest of links to repo.
1 parent 23b6624 commit 094bcd3

File tree

1 file changed

+0
-76
lines changed

1 file changed

+0
-76
lines changed

articles/cosmos-db/mongodb/quickstart-python.md

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -170,41 +170,13 @@ Check if the database exists with [list_database_names](https://pymongo.readthed
170170
171171
Check if the collection exists with the [list_collection_names](https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html#pymongo.database.Database.list_collection_names) method. If the collection doesn't exist, use the [create collection extension command](/azure/cosmos-db/mongodb/custom-commands#create-collection) to create it.
172172
173-
```python
174-
# Create collection if it doesn't exist
175-
collection = db[COLLECTION_NAME]
176-
if COLLECTION_NAME not in db.list_collection_names():
177-
# Creates a unsharded collection that uses the DBs shared throughput
178-
db.command({"customAction": "CreateCollection", "collection": COLLECTION_NAME})
179-
print("Created collection '{}'.\n".format(COLLECTION_NAME))
180-
else:
181-
print("Using collection: '{}'.\n".format(COLLECTION_NAME))
182-
```
183-
<!---
184173
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/run.py" id="new_collection":::
185-
--->
186174
187175
### Create an index
188176
189177
Create an index using the [update collection extension command](/azure/cosmos-db/mongodb/custom-commands#update-collection). You can also set the index in the create collection extension command. Set the index to `name` property in this example so that you can later sort with the cursor class [sort](https://pymongo.readthedocs.io/en/stable/api/pymongo/cursor.html#pymongo.cursor.Cursor.sort) method on product name.
190178
191-
```python
192-
indexes = [
193-
{"key": {"_id": 1}, "name": "_id_1"},
194-
{"key": {"name": 2}, "name": "_id_2"},
195-
]
196-
db.command(
197-
{
198-
"customAction": "UpdateCollection",
199-
"collection": COLLECTION_NAME,
200-
"indexes": indexes,
201-
}
202-
)
203-
print("Indexes are: {}\n".format(sorted(collection.index_information())))
204-
```
205-
<!---
206179
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/index.js" id="create_index":::
207-
--->
208180
209181
### Create a document
210182
@@ -216,51 +188,21 @@ Create a document with the *product* properties for the `adventureworks` databas
216188
* An inventory *quantity* property.
217189
* A *sale* property, indicating whether the product is on sale.
218190
219-
```python
220-
"""Create new document and upsert (create or replace) to collection"""
221-
product = {
222-
"category": "gear-surf-surfboards",
223-
"name": "Yamba Surfboard-{}".format(randint(50, 5000)),
224-
"quantity": 1,
225-
"sale": False,
226-
}
227-
result = collection.update_one(
228-
{"name": product["name"]}, {"$set": product}, upsert=True
229-
)
230-
print("Upserted document with _id {}\n".format(result.upserted_id))
231-
```
232-
<!---
233191
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/index.js" id="new_doc":::
234-
--->
235192
236193
Create a document in the collection by calling the collection level operation [update_one](https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.update_one). In this example, we chose to *upsert* instead of *create* a new document in case you run this sample code more than once.
237194
238195
### Get a document
239196
240197
In Azure Cosmos DB, you can perform a less-expensive [point read](https://devblogs.microsoft.com/cosmosdb/point-reads-versus-queries/) operation by using both the unique identifier (`_id`) and partition key (`category`).
241198
242-
```python
243-
doc = collection.find_one({"_id": result.upserted_id})
244-
print("Found a document with _id {}: {}\n".format(result.upserted_id, doc))
245-
```
246-
<!---
247199
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/index.js" id="read_doc":::
248-
--->
249200
250201
### Query documents
251202
252203
After you insert a doc, you can run a query to get all docs that match a specific filter. This example finds all docs that match a specific category: `gear-surf-surfboards`. Once the query is defined, call [`Collection.find`](https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find) to get a [`Cursor`](https://pymongo.readthedocs.io/en/stable/api/pymongo/cursor.html#pymongo.cursor.Cursor) result.
253204
254-
```python
255-
"""Query for documents in the collection"""
256-
print("Products with category 'gear-surf-surfboards':\n")
257-
allProductsQuery = {"category": "gear-surf-surfboards"}
258-
for doc in collection.find(allProductsQuery).sort("name", pymongo.ASCENDING):
259-
print("Found a product with _id {}: {}\n".format(doc["_id"], doc))
260-
```
261-
<!---
262205
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/index.js" id="query_doc":::
263-
--->
264206
265207
Troubleshooting:
266208
@@ -278,25 +220,7 @@ python run.py
278220

279221
The output of the app should be similar to this example:
280222

281-
```console
282-
Created db 'adventureworks' with shared throughput.
283-
284-
Created collection 'products'.
285-
286-
Indexes are: ['_id_', 'name_1']
287-
288-
Upserted document with _id <ID>
289-
290-
Found a document with _id <ID>: {'_id': <ID>, 'category': 'gear-surf-surfboards', 'name': 'Yamba Surfboard-50', 'quantity': 1, 'sale': False}
291-
292-
Products with category 'gear-surf-surfboards':
293-
294-
Found a product with _id <ID>: {'_id': ObjectId('<ID>'), 'name': 'Yamba Surfboard-386', 'category': 'gear-surf-surfboards', 'quantity': 1, 'sale': False}
295-
```
296-
297-
<!---
298223
:::code language="python" source="~/azure-cosmos-db-mongodb-python-getting-started/001-quickstart/index.js" id="console_result":::
299-
--->
300224

301225
## Clean up resources
302226

0 commit comments

Comments
 (0)