Skip to content

Commit 6f9de77

Browse files
authored
Merge pull request #18 from mongodb-developer/add-dotnet
2 parents 829e0bc + 6c4d5b9 commit 6f9de77

18 files changed

+3339
-4
lines changed

.devcontainer/devcontainer.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,31 @@
1010
"installMaven": "true",
1111
"mavenVersion": "3.9.10"
1212
},
13-
"ghcr.io/devcontainers/features/python:1": {}
13+
// https: //github.com/devcontainers/features/tree/main/src/python
14+
"ghcr.io/devcontainers/features/python:1": {
15+
"version": "3.12"
16+
},
17+
"ghcr.io/devcontainers/features/dotnet:2.3.0": {
18+
"version": "9.0",
19+
"installUsingApt": true
20+
}
1421
},
1522
"updateContentCommand": "bash .devcontainer/install-mongodb-db-tools.sh && bash .devcontainer/setup.sh && bash .devcontainer/import.sh",
1623
"postCreateCommand": "",
1724
"postAttachCommand": "",
18-
"postStartCommand": "jupyter trust java/*.ipynb && jupyter trust javascript/*.ipynb && jupyter trust python/*.ipynb && jupyter notebook .",
25+
"postStartCommand": "jupyter trust java/*.ipynb && jupyter trust javascript/*.ipynb && jupyter trust python/*.ipynb && jupyter trust dotnet/*.ipynb && jupyter notebook dotnet/*.ipynb",
1926
"customizations": {
2027
"codespaces": {
2128
"openFiles": [
22-
"README.md"
29+
"dotnet/00_open_mongodb.ipynb"
2330
]
2431
},
2532
"vscode": {
2633
"extensions": [
2734
"ms-toolsai.jupyter",
28-
"mongodb.mongodb-vscode"
35+
"mongodb.mongodb-vscode",
36+
"ms-dotnettools.csharp",
37+
"ms-dotnettools.dotnet-interactive-vscode"
2938
]
3039
}
3140
},

.devcontainer/setup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ echo ✅ -----------------------------
2424
sudo apt-get install python3-pymongo -y
2525
sudo apt-get clean packages
2626

27+
# Dotnet Kernel
28+
sudo dotnet tool install -g Microsoft.dotnet-interactive
29+
sudo dotnet interactive jupyter install
30+
2731
echo ✅ Install Deno
2832
echo ✅ ------------
2933
# Install deno - Nodejs modern engine

dotnet/00_open_mongodb.ipynb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Open MongoDB Using the MongoDB Extension\n",
8+
"\n",
9+
"- click on the MongoDB extension on the side panel\n",
10+
"- add a new connection clicking on `Add Connection`\n",
11+
"- use as connection:"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {
18+
"vscode": {
19+
"languageId": "plaintext"
20+
}
21+
},
22+
"outputs": [],
23+
"source": [
24+
"mongodb://admin:mongodb@localhost:27017/"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"You should see four databases in the MongoDB database server:\n",
32+
"- admin\n",
33+
"- config\n",
34+
"- local\n",
35+
"- library -> we're going to work with this one"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Open the library database\n",
43+
"\n",
44+
"- open the library database\n",
45+
"- open the books collection\n",
46+
"- click on Documents\n",
47+
"- open a document"
48+
]
49+
}
50+
],
51+
"metadata": {
52+
"language_info": {
53+
"name": "python"
54+
}
55+
},
56+
"nbformat": 4,
57+
"nbformat_minor": 2
58+
}

dotnet/01_connect_database.ipynb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "409549ea",
6+
"metadata": {},
7+
"source": [
8+
"# Install the MongoDB Driver"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "d3de619e",
15+
"metadata": {
16+
"polyglot_notebook": {
17+
"kernelName": "csharp"
18+
}
19+
},
20+
"outputs": [],
21+
"source": [
22+
"#r \"nuget:MongoDB.Driver\"\n",
23+
"\n",
24+
"using MongoDB.Driver;\n",
25+
"using MongoDB.Bson;"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"id": "22d7c5ea",
31+
"metadata": {},
32+
"source": [
33+
"# Create new client and run a basic command to test connection"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"id": "53fb64de",
40+
"metadata": {
41+
"polyglot_notebook": {
42+
"kernelName": "csharp"
43+
}
44+
},
45+
"outputs": [],
46+
"source": [
47+
"string connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
48+
"\n",
49+
"MongoClient client = new MongoClient(connectionString);\n",
50+
"var result = client.GetDatabase(\"admin\").RunCommand<BsonDocument>(new BsonDocument(\"ping\", 1));\n",
51+
"Console.WriteLine(\"MongoDB server version: \" + result.ToJson());\n"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"id": "03c03c08",
57+
"metadata": {},
58+
"source": [
59+
"# List all databases and collections in the cluster"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {
66+
"language_info": {
67+
"name": "polyglot-notebook"
68+
},
69+
"polyglot_notebook": {
70+
"kernelName": "csharp"
71+
}
72+
},
73+
"outputs": [],
74+
"source": [
75+
"var dbs = client.ListDatabaseNames().ToList();\n",
76+
"\n",
77+
"foreach(var db in dbs)\n",
78+
"{\n",
79+
" Console.WriteLine(db);\n",
80+
" var database = client.GetDatabase(db); \n",
81+
" var collectionNames = database.ListCollectionNames().ToList(); \n",
82+
"\n",
83+
" foreach (var collName in collectionNames) \n",
84+
" { \n",
85+
" Console.WriteLine($\" - {collName}\"); \n",
86+
" } \n",
87+
"}"
88+
]
89+
}
90+
],
91+
"metadata": {
92+
"kernelspec": {
93+
"display_name": ".NET (C#)",
94+
"language": "C#",
95+
"name": ".net-csharp"
96+
},
97+
"language_info": {
98+
"name": "polyglot-notebook"
99+
},
100+
"polyglot_notebook": {
101+
"kernelInfo": {
102+
"defaultKernelName": "csharp",
103+
"items": [
104+
{
105+
"aliases": [],
106+
"name": "csharp"
107+
}
108+
]
109+
}
110+
}
111+
},
112+
"nbformat": 4,
113+
"nbformat_minor": 5
114+
}

0 commit comments

Comments
 (0)