Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions java/00_open_mongodb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"- admin\n",
"- config\n",
"- local\n",
"- library -> __we're going to work with this one__\n",
"- library_with_embedding -> same data, but books have vector embedding for vector search"
"- library -> __we're going to work with this one__"
]
},
{
Expand Down
53 changes: 42 additions & 11 deletions java/01_connect_database.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
"cell_type": "code",
"execution_count": null,
"id": "66ef28c4-f86b-4576-839e-100a7ae022c7",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
Expand All @@ -75,7 +79,11 @@
"cell_type": "code",
"execution_count": null,
"id": "urban-boston",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";"
Expand All @@ -88,14 +96,18 @@
"source": [
"## Ping the Database server\n",
"\n",
"The ping() database command fetches information about the state of the MongoDB database."
"The `ping()` database command fetches information about the state of the MongoDB database."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "textile-spokesman",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"MongoClient mongoClient = null;\n",
Expand Down Expand Up @@ -125,7 +137,11 @@
"cell_type": "code",
"execution_count": null,
"id": "00dd063a",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"try {\n",
Expand All @@ -142,7 +158,7 @@
"id": "amazing-webcam",
"metadata": {},
"source": [
"## List all databases in the cluster\n",
"## Get a list of all databases and collections\n",
"\n",
"We'll get a list of all the databases in my cluster using `listDatabases()` and then we'll print them."
]
Expand All @@ -152,20 +168,35 @@
"execution_count": null,
"id": "annoying-tattoo",
"metadata": {
"scrolled": true
"scrolled": true,
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"System.out.println(\"=> Print list of databases\");\n",
"List<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());\n",
"databases.forEach(db -> System.out.println(db.toJson()));"
"databases.forEach(dbDoc -> {\n",
" String dbName = dbDoc.getString(\"name\");\n",
" MongoDatabase db = mongoClient.getDatabase(dbName);\n",
"\n",
" db.listCollectionNames().forEach(collectionName -> \n",
" System.out.println(dbName + \".\" + collectionName)\n",
" );\n",
" }\n",
");"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19f3150c",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": []
}
Expand All @@ -180,9 +211,9 @@
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "java",
"name": "Java",
"pygments_lexer": "java",
"version": "21.0.5+11-LTS"
"version": "21.0.8+9-LTS"
}
},
"nbformat": 4,
Expand Down
114 changes: 59 additions & 55 deletions java/100_aggregation_pipeline_match.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,54 +44,14 @@
"import com.mongodb.client.MongoCollection;\n",
"import com.mongodb.client.MongoDatabase;\n",
"import com.mongodb.client.model.Accumulators;\n",
"import com.mongodb.client.model.Aggregates;\n",
"import com.mongodb.client.model.Filters;\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
"import java.util.Arrays;\n",
"\n",
"import java.util.Arrays;"
]
},
{
"cell_type": "markdown",
"id": "advisory-christmas",
"metadata": {},
"source": [
"## Set your connection String below"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "urban-boston",
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";"
]
},
{
"cell_type": "markdown",
"id": "13926b8c",
"metadata": {},
"source": [
"## $match"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af4eccbc",
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
"\n",
"// Define our database and collection. We'll use the `library` variable that points to our Database and `books` that points to the collection we're using.\n",
"MongoClient mongoClient = null;\n",
"try {\n",
" // connect to MongoDB\n",
Expand All @@ -109,7 +69,9 @@
"id": "handled-symbol",
"metadata": {},
"source": [
"### $match"
"## $match\n",
"\n",
"We'll get all book written after 2010"
]
},
{
Expand All @@ -123,11 +85,16 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
" Aggregates.match(Filters.and(\n",
" Filters.eq(\"year\", 2010)\n",
"import static com.mongodb.client.model.Aggregates.*;\n",
"import static com.mongodb.client.model.Filters.*;\n",
"\n",
"List<Bson> booksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" ))\n",
"));\n",
");\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(booksFrom2010);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
Expand All @@ -140,7 +107,9 @@
"id": "9f6caab3",
"metadata": {},
"source": [
"### $limit"
"### $limit\n",
"\n",
"Now we want to limit just to 10 books"
]
},
{
Expand All @@ -153,7 +122,24 @@
}
},
"outputs": [],
"source": []
"source": [
"import static com.mongodb.client.model.Aggregates.*;\n",
"import static com.mongodb.client.model.Filters.*;\n",
"\n",
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" )),\n",
" limit(10)\n",
");\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
]
},
{
"cell_type": "markdown",
Expand All @@ -173,7 +159,25 @@
}
},
"outputs": [],
"source": []
"source": [
"import static com.mongodb.client.model.Aggregates.*;\n",
"import static com.mongodb.client.model.Filters.*;\n",
"\n",
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" )),\n",
" limit(10),\n",
" sort(new Document(\"year\", 1))\n",
");\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
]
}
],
"metadata": {
Expand All @@ -188,7 +192,7 @@
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "21.0.5+11-LTS"
"version": "21.0.8+9-LTS"
}
},
"nbformat": 4,
Expand Down
18 changes: 9 additions & 9 deletions java/10_find.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"id": "9f6caab3",
"metadata": {},
"source": [
"## Exercise: find all the books that have less than 50 pages and project only the title and pages"
"## Find all the books that have less than 50 pages and project only the title and pages"
]
},
{
Expand Down Expand Up @@ -270,15 +270,15 @@
},
"outputs": [],
"source": [
"// type in your code here, you'll need to adapt the code a bit\n"
"// type in your code here\n"
]
},
{
"cell_type": "markdown",
"id": "627c4fb7",
"metadata": {},
"source": [
"## Find all books with more than 300 pages.\n",
"### Find all books longer than 300 pages.\n",
"\n",
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/WHERE#2-find-all-books-with-more-than-300-pages)"
]
Expand All @@ -289,20 +289,20 @@
"id": "fd8a8143",
"metadata": {
"vscode": {
"languageId": "markdown"
"languageId": "java"
}
},
"outputs": [],
"source": [
"// type in your code here, you'll need to adapt the code a bit\n"
"// type in your code here\n"
]
},
{
"cell_type": "markdown",
"id": "601d0b12",
"metadata": {},
"source": [
"## Find books in the Science genre that are more than 300 pages long.\n",
"### Find books in the Science genre that are longer than 300 pages long.\n",
"\n",
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/WHERE#3-find-books-in-the-science-genre-that-are-more-than-300-pages-long)"
]
Expand All @@ -313,12 +313,12 @@
"id": "1a94638b",
"metadata": {
"vscode": {
"languageId": "markdown"
"languageId": "java"
}
},
"outputs": [],
"source": [
"// type in your code here, you'll need to adapt the code a bit\n"
"// type in your code here\n"
]
}
],
Expand All @@ -334,7 +334,7 @@
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "21.0.5+11-LTS"
"version": "21.0.8+9-LTS"
}
},
"nbformat": 4,
Expand Down
Loading