|
| 1 | +--- |
| 2 | +slug: connect-mongodb-with-windmill |
| 3 | +title: How to Connect MongoDB Atlas with Windmill |
| 4 | +authors: [adamkov] |
| 5 | +tags: [mongodb, windmill, resource, setup, integrate, connect, v2] |
| 6 | +image: ./0-header.png |
| 7 | +--- |
| 8 | + |
| 9 | +This guide aims to show you how to create a connection from your Windmill |
| 10 | +instance to an external MongoDB Atlas database, then use it to make queries. |
| 11 | + |
| 12 | +<!--truncate--> |
| 13 | + |
| 14 | +:::info |
| 15 | + |
| 16 | +[MongoDB Atlas][mongodb-atlas] is the cloud-hosted and managed version of |
| 17 | +MongoDB. This guide **won't cover** the self-hosted version of MongoDB and also |
| 18 | +assumes, that you already have an Atlas database set up. |
| 19 | + |
| 20 | +You can find more information about setting up MongoDB Atlas |
| 21 | +[here][mongodb-atlas-setup]. |
| 22 | + |
| 23 | +::: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +## Create Resource |
| 28 | + |
| 29 | +Windmill provides integration with many different apps and services with the use |
| 30 | +of [Resources][docs-resources]. Each Resource has a **Resources Type**, which |
| 31 | +controls the shape of it. To be able to connect to a MongoDB instance, we'll |
| 32 | +need to define a Resource with the `mongodb_rest` Resource Type first. |
| 33 | + |
| 34 | +:::tip |
| 35 | + |
| 36 | +You can find a list of all the officially supported Resource Types on |
| 37 | +[Windmill Hub][hub-resources]. |
| 38 | + |
| 39 | +::: |
| 40 | + |
| 41 | +Head to the [Resources][wm-app-resources] page in the Windmill app, click on |
| 42 | +"Add a resource/API" in the top right corner and select the `mongodb_rest` type. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +:::caution |
| 47 | + |
| 48 | +There is a `mongodb` and a `mongodb_rest` Resource Type. Make sure that you |
| 49 | +select **`mongodb_rest`** as this will make it easier to connect to MongoDB |
| 50 | +Atlas. |
| 51 | + |
| 52 | +::: |
| 53 | + |
| 54 | +To enable access to your database, follow the instructions in |
| 55 | +[this article][mongo-api] and paste your **API key** and **endpoint** in |
| 56 | +Windmill. When it's done, click "Save". |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Create Script |
| 61 | + |
| 62 | +Next, let's create a Script that will use the newly created Resource. Head on to |
| 63 | +the [Home][wm-app-home] page and click on the "+Script" button. We'll be using |
| 64 | +TypeScript as the language. |
| 65 | + |
| 66 | +:::info |
| 67 | + |
| 68 | +Windmill uses Deno as the TypeScript runtime. |
| 69 | + |
| 70 | +::: |
| 71 | + |
| 72 | +Name the Script `my_mongodb_script`, give it a summary, "Query the Example |
| 73 | +MongoDB Dataset" for example and click "Next". |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +Paste in the following code into the editor: |
| 78 | + |
| 79 | +```typescript |
| 80 | +import { Resource } from "https://deno.land/x/windmill@v1.55.0/mod.ts"; |
| 81 | +import { MongoClient } from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts"; |
| 82 | + |
| 83 | +export async function main( |
| 84 | + auth: Resource<"mongodb_rest">, |
| 85 | + data_source: string, |
| 86 | + database: string, |
| 87 | + collection: string, |
| 88 | + filter: Record<string, any>, |
| 89 | +) { |
| 90 | + const client = new MongoClient({ |
| 91 | + endpoint: auth.endpoint, |
| 92 | + dataSource: data_source, |
| 93 | + auth: { apiKey: auth.api_key }, |
| 94 | + }); |
| 95 | + const documents = client.database(database).collection(collection); |
| 96 | + return await documents.find(filter); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +In case you are using the [sample dataset][mongo-sample-data] of MongoDB Atlas, |
| 101 | +you'll have a `sample_restaurants` database filled with restarurants. To make a |
| 102 | +query for a specific restaurant name, the arguments of the Script would look |
| 103 | +like the followings (**casing matters**): |
| 104 | + |
| 105 | +- **auth** - select the Resource we created in the previous step |
| 106 | + (`my_mongodb_rest`) |
| 107 | +- **data_source** - `Cluster0` |
| 108 | +- **database** - `sample_restaurants` |
| 109 | +- **collection** - `restaurants` |
| 110 | +- **filter** - `{ "name": "Nordic Delicacies" }` |
| 111 | + |
| 112 | +After filling the inputs, try running the Script by clicking "Test" or pressing |
| 113 | +`Ctrl` + `Enter`. You should see exactly one restaurant returned in the bottom |
| 114 | +right corner. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +If you tried querying by the `_id` field, you might have noticed that it didn't |
| 119 | +return anything. That's because it is stored as an ObjectID, which is a special |
| 120 | +type in MongoDB. To query by ID, you'll need to convert the filter value to an |
| 121 | +ObjectID first. Replace your code with the following to make it able to query by |
| 122 | +ID: |
| 123 | + |
| 124 | +```typescript |
| 125 | +import { Resource } from "https://deno.land/x/windmill@v1.55.0/mod.ts"; |
| 126 | +import { |
| 127 | + MongoClient, |
| 128 | + ObjectId, |
| 129 | +} from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts"; |
| 130 | + |
| 131 | +export async function main( |
| 132 | + auth: Resource<"mongodb_rest">, |
| 133 | + data_source: string, |
| 134 | + database: string, |
| 135 | + collection: string, |
| 136 | + filter: Record<string, any>, |
| 137 | +) { |
| 138 | + const client = new MongoClient({ |
| 139 | + endpoint: auth.endpoint, |
| 140 | + dataSource: data_source, |
| 141 | + auth: { apiKey: auth.api_key }, |
| 142 | + }); |
| 143 | + const documents = client.database(database).collection(collection); |
| 144 | + if ("_id" in filter) { |
| 145 | + filter["_id"] = new ObjectId(filter["_id"]); |
| 146 | + } |
| 147 | + return await documents.find(filter); |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +Now try running the Script again with the same arguments, **except for the |
| 152 | +filter**, which should be `{ "_id": "5eb3d668b31de5d588f4293b" }`. You should |
| 153 | +see the same restaurant named "Nordic Delicacies" returned. |
| 154 | + |
| 155 | +:::tip |
| 156 | + |
| 157 | +You can find more Script examples related to MongoDB on |
| 158 | +[Windmill Hub][hub-mongo]. |
| 159 | + |
| 160 | +::: |
| 161 | + |
| 162 | +Once you're done, click on "Save", which will save it to your workspace. You can |
| 163 | +now use this Script in your [Flows][docs-flows], [Apps][docs-apps] or as |
| 164 | +standalone. |
| 165 | + |
| 166 | +<!-- Links --> |
| 167 | + |
| 168 | +[wm-app-resources]: https://app.windmill.dev/resources |
| 169 | +[wm-app-home]: https://app.windmill.dev |
| 170 | +[hub-resources]: https://hub.windmill.dev/resources |
| 171 | +[hub-mongo]: https://hub.windmill.dev/integrations/mongodb |
| 172 | +[docs-resources]: /docs/core_concepts/resources_and_types |
| 173 | +[docs-path]: /docs/reference#path |
| 174 | +[docs-flows]: /docs/getting_started/flows_quickstart |
| 175 | +[docs-apps]: /docs/getting_started/apps_quickstart |
| 176 | +[mongodb-atlas]: https://www.mongodb.com/atlas/database |
| 177 | +[mongodb-atlas-setup]: https://www.mongodb.com/basics/mongodb-atlas-tutorial |
| 178 | +[mongo-api]: https://www.mongodb.com/docs/atlas/api/data-api/ |
| 179 | +[mongo-sample-data]: https://www.mongodb.com/docs/atlas/sample-data/ |
0 commit comments