Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions python/20_project_sort_limit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"outputs": [],
"source": [
"# Import the MongoDB Driver\n",
"from pymongo import MongoClient\n",
"from pymongo import MongoClient, ASCENDING, DESCENDING\n",
"\n",
"# Set your connection String\n",
"mongoDBURI = \"mongodb://admin:mongodb@localhost:27017/?directConnection=true\";\n",
Expand All @@ -52,7 +52,7 @@
"id": "4",
"metadata": {},
"source": [
"## Sort books by descending number of pages"
"## Sort books by ascending number of pages"
]
},
{
Expand All @@ -67,7 +67,33 @@
" \"pages\": 1\n",
"}\n",
"\n",
"cursor = books.find({}, projection).sort(\"pages\", -1).limit(10)\n",
"cursor = books.find({}, projection).sort(\"pages\", ASCENDING).limit(10)\n",
"\n",
"for book in cursor:\n",
" print(book)"
]
},
{
"cell_type": "markdown",
"id": "bd686877",
"metadata": {},
"source": [
"## Sort books by descending number of pages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40544ad2",
"metadata": {},
"outputs": [],
"source": [
"projection = {\n",
" \"title\": 1,\n",
" \"pages\": 1\n",
"}\n",
"\n",
"cursor = books.find({}, projection).sort(\"pages\", DESCENDING).limit(10)\n",
"\n",
"for book in cursor:\n",
" print(book)"
Expand Down