Skip to content

Latest commit

 

History

History
65 lines (43 loc) · 1.72 KB

File metadata and controls

65 lines (43 loc) · 1.72 KB
ms.custom devx-track-js
ms.topic include
ms.date 03/04/2021

Install mongodb SDK

To connect and use your mongoDB on Azure Cosmos DB with JavaScript and mongodb, use the following procedure.

  1. Make sure Node.js and npm are installed.

  2. Create a Node.js project in a new folder:

    mkdir DataDemo && \
        cd DataDemo && \
        npm init -y && \
        npm install mongodb &&
        code .

    The command:

    • Creates a project folder named DataDemo
    • Changes the Bash terminal into that folder
    • Initializes the project, which creates the package.json file
    • Installs the SDK
    • Opens the project in Visual Studio Code

Create JavaScript file to bulk insert data into MongoDB database

  1. In Visual Studio Code, create a bulk_insert.js file.

  2. Download the MOCK_DATA.csv file and place it in the same directory as bulk_insert.js.

  3. Copy the following JavaScript code into bulk_insert.js:

    :::code language="javascript" source="~/../js-e2e//database/mongodb/bulk_insert_mongodb.js" :::

  4. Replace the following in the script with your resource information:

    • YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
  5. Run the script.

    node bulk_insert.js

Create JavaScript code to use MongoDB

  1. In Visual Studio Code, create a index.js file.

  2. Copy the following JavaScript code into index.js:

    :::code language="javascript" source="~/../js-e2e/database/mongodb/index_mongodb.js" :::

  3. Replace the following in the script with your resource information:

    • YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
  4. Run the script.

    node index.js