Getting Started navigation (v0.114-0)

You are viewing the frozen documentation for v0.114-0. It is not updated and may not reflect the latest release.

View current version →

Node.js Setup Guide

Learn how to set up and use DocumentDB with Node.js using the official MongoDB Node.js driver.

Prerequisites

  • Node.js 14.x or later
  • npm or yarn package manager
  • DocumentDB installed and running
  • Docker installed (if set up is not completed yet)
  • Basic Node.js knowledge

Project Setup (skip if already done)

Before connecting from Node.js, make sure you have a running DocumentDB instance using Docker:

# Pull the latest DocumentDB Docker image
docker pull ghcr.io/documentdb/documentdb/documentdb-local:latest

# Tag the image for convenience
docker tag ghcr.io/documentdb/documentdb/documentdb-local:latest documentdb

# Run the container with your chosen username and password
docker run -dt -p 10260:10260 --name documentdb-container documentdb --username <YOUR_USERNAME> --password <YOUR_PASSWORD>
docker image rm -f ghcr.io/documentdb/documentdb/documentdb-local:latest

Note: During the transition to the Linux Foundation, Docker images may still be hosted on Microsoft's container registry. These will be migrated to the new DocumentDB organization as the transition completes.

Note: Replace <YOUR_USERNAME> and <YOUR_PASSWORD> with your desired credentials. Always set them explicitly: if you omit them the container starts with the built-in default_user / Admin100, which are public and let anyone who can reach the published port authenticate as the admin user.

Readiness Note: docker ps reports the container as Up before DocumentDB can accept connections. Wait for the ready banner first: until docker logs documentdb-container 2>&1 | grep -q "=== DocumentDB is ready ==="; do sleep 2; done

Port Note: Port 10260 is used by default in these instructions to avoid conflicts with other local database services. You can use port 27017 (the standard MongoDB port) or any other available port if you prefer. If you do, be sure to update the port number in both your docker run command and your connection string accordingly.

Installation

  1. Creating a new Node.js project

    mkdir my-documentdb-app
    cd my-documentdb-app
    npm init -y
  2. Installing the MongoDB driver

    npm install mongodb

Connecting to DocumentDB

DocumentDB Local accepts TLS connections on the gateway port and requires authentication. Connect with the username and password you set when starting the container, and because the container uses a self-signed certificate, the simplest local setup skips certificate validation with tlsAllowInvalidCertificates=true (in production, provide the gateway certificate instead).

const { MongoClient } = require('mongodb');

const uri = 'mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true';
const client = new MongoClient(uri);

async function connect() {
  try {
    await client.connect();
    const db = client.db('your_database');
    return db;
  } catch (error) {
    console.error('Connection error:', error);
    throw error;
  }
}

Basic Operations

  1. Creating collections

    const collection = db.collection('your_collection');
  2. Document operations

    • Insert operations
    • Find operations
    • Update operations
    • Delete operations

Working with Promises and Async/Await

  1. Promise-based operations
  2. Async/await patterns
  3. Error handling
  4. Connection management

Advanced Features

  1. Bulk operations
  2. Aggregation framework
  3. Vector search
  4. Geospatial queries
  5. Change streams
  6. Transactions

Error Handling

  1. Connection errors
  2. Operation errors
  3. Timeout handling
  4. Retry strategies

Best Practices

  1. Connection pooling
  2. Query optimization
  3. Bulk operations
  4. Error handling
  5. Security considerations

Sample Applications

  1. Basic CRUD application
  2. REST API with Express
  3. Vector search example
  4. Real-time applications with change streams

Testing

  1. Setting up test environment
  2. Unit testing with Jest/Mocha
  3. Integration testing
  4. Mock testing

Deployment

  1. Development setup
  2. Production considerations
  3. Monitoring and logging
  4. Performance optimization

Next Steps

  • Explore advanced features
  • Learn about indexing strategies
  • Build your first application