| ms.custom | devx-track-js |
|---|---|
| ms.topic | include |
| ms.date | 03/04/2021 |
To connect and use your mongoDB on Azure Cosmos DB with JavaScript and mongoose, use the following procedure.
-
Make sure Node.js and npm are installed.
-
Create a Node.js project in a new folder:
mkdir DataDemo && \ cd DataDemo && \ npm init -y && \ npm install mongoose && code .
The command:
- Creates a project folder named
DataDemo - Changes the Bash terminal into that folder
- Initializes the project, which creates the
package.jsonfile - Installs the SDK
- Opens the project in Visual Studio Code
- Creates a project folder named
-
Copy the following JavaScript code into
index.js: -
:::code language="javascript" source="~/../js-e2e//database/mongodb/index_mongoose.js" :::
-
Replace
YOUR-CONNECTION-STRINGin the script with your resource connection string. -
Run the script.
node index.js
The results are:
find all loop {"_id":"6019a68a6ecddc35d536c92c","name":"Joan Smith","job":"Developer","__v":0} loop {"_id":"6019a68e6ecddc35d536c92d","name":"Bob Jones","job":"Quality Assurance","__v":0} loop {"_id":"6019a6916ecddc35d536c92e","name":"Michelle Roberts","job":"Program Manager","__v":0} succeeded