Sling Academy
Home/Node.js/How to Perform Bulk Upsert in Mongoose

How to Perform Bulk Upsert in Mongoose

Last updated: December 30, 2023

Overview

Bulk upsert operations in Mongoose are an efficient way to update multiple documents within a MongoDB collection while inserting any new documents if they do not already exist. This tutorial will walk you through the basics and into more advanced use cases for bulk upserts using Mongoose with Node.js.

Before diving into bulk upserts, ensure you have Node.js installed, familiarity with JavaScript or TypeScript, and a MongoDB database to work with.

Defining Mongoose Schema

Start by setting up a Node.js project and install Mongoose:

npm init -y
npm install mongoose

Then, create your Mongoose schema and model:

import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
  username: String,
  email: String,
  meta: {
    likes: Number
  }
});

const User = mongoose.model('User', userSchema);

Performing a Basic Bulk Upsert

Use the bulkWrite method to execute a simple upsert:

const bulkOperation = await User.bulkWrite([
  {
    updateOne: {
      filter: { username: 'johndoe' },
      update: {$set: { email: '[email protected]' }},
      upsert: true
    }
  },
  // ... More operations
]);

Handling Complex Upserts

For bulk upserting with more complex logic:

const complexBulkOperation = await User.bulkWrite([
  {
    updateOne: {
      filter: { username: 'janedoe', 'meta.likes': {$lt: 100} },
      update: {
        $setOnInsert: { email: '[email protected]' },
        $inc: { 'meta.likes': 1 }
      },
      upsert: true
    }
  }
  // ... More complex operations
]);

Using Async/Await with Bulk Upserts

Integrate async/await for better flow control:

async function runBulkUpsert() {
  try {
    const result = await User.bulkWrite([...]);
    console.log('Bulk upsert success:', result);
  } catch (error) {
    console.error('Bulk upsert failed:', error);
  }
}

runBulkUpsert();

Error Handling and Validation

Be aware of the potential errors and how to handle them:

async function runSafeBulkUpsert() {
  try {
    // Perform bulk upsert
  } catch (error) {
    if (error.name === 'ValidationError') {
      // Handle validation errors
    } else {
      // Handle other errors
    }
  }
}

runSafeBulkUpsert();

Summary

This tutorial covered the essentials of performing bulk upsert operations using Mongoose. By understanding and implementing these patterns, you can significantly optimize your MongoDB interactions within a Node.js application.

Next Article: How to prevent injection attacks in Mongoose

Previous Article: How to convert a Mongoose document to JSON

Series: Mongoose.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates