MongoDb + Mongoose | How to hash array of passwords instead of a single password

mongodb tutorial
mongodb download
mongodb documentation
mongodb atlas
mongodb install
mongodb compass
mongodb vs mysql
mongodb university

I have a json array containing data of almost 300 users. I am using (Mongoose)Model.InsertMany() for saving user data array to Mongodb. To hash a single user password i am using this guide:

but i want to hash passwords of all users at once. This guide uses 'save' function to hash but as i am using 'InsertMany()' to dump to Mongodb so how can i achieve hashing by using InsertMany()

You can use pre save hook in your user model.

The problem is InsertMany does not work with pre save hook.

But using Model.create will call any hooks declared on your schema.

So you can use User.create() method to make it work.

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true
  password: {
    type: String,
    required: true

userSchema.pre('save', async function(next) {
  this.password = await bcrypt.hash(this.password, 12);

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

module.exports = User;

MongoDB, MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on� MongoDB is a document database, which means it stores data in JSON-like documents. We believe this is the most natural way to think about data, and is much more expressive and powerful than the traditional row/column model.

Mongoose insertMany() doesn't trigger the .save() hook. So, you should hash all the passwords first then use insertMany() function to insert them into database.

MongoDB Tutorial, It's happening! Join us today at 11am EST LIVE on @twitch with @joekarlsson1 and @wayneoflife! Tune in here: Find the guides, samples, and references you need to use the database, visualize data, and build applications on the MongoDB data platform. Run MongoDB in the cloud Work with your data in a fully-managed database on AWS, Azure, and

you have to create pre hooks on mongoose that will call before creating a document, in pre hook function you can use bcrypt a node library generate a hash string.

MongoDB ✓, Join us on Thursday (7/9), for a virtual session with @manuelreil from @alyne to learn about using MongoDB @realm with Atlas! Visit the link to register: MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of key-value pairs which are the basic unit of data in MongoDB.

mongodb/mongo: The MongoDB Database, In this Quick Start tutorial by @judy2k, learn how to connect your Rust application to a MongoDB cluster, create, read, update, and delete (CRUD) operations on a collection, and use serde to map between our BSON documents and Rust structs. MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.

MongoDB in 5 Minutes with Eliot Horowitz, Indexes are great (seriously!), but it's easy to get carried away and make indexes that you'll never actually use. In this article from @lauren_schaefer, examine why an index may be unnecessary and what the consequences of keeping it around are. MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.

Happening on July 15th, join us for a webinar and demo to learn why MongoDB Atlas is the best way to deploy, manage & grow MongoDB on @gcpcloud. Visit the link to register now: Free, Digital, Global Join us for our two-day online event and be the first to know what we're building next.

  • Please don't forget to mark my answer as answer to show that your question is resolved
  • i am using save() for a single entry (and it is working for a single entry) but for 300 passwords at once i am using Insertmany(). How can i hash 300 passwords at once?
  • InsertMany() does not trigger pre save hook
  • Exactly ! insertMany() doesn't trigger the .save() hook. As i am a beginner, can you please tell me how can i hash 300 passwords ? Is there a shortcut or do i have to do it one by one. Because it is working for a single document as i am using .save for it
  • i am using pre hooks and it is working for a single entry but how about an array of passwords. how to hash all of them at once as i am using InsertMany() instead of save()
  • insertMany !== save. If you want pre save hooks to be called, use create() or in insertMany you have to iterate the array and assign a hashed password and then user insertMany
  • I am a beginner so can you write some code for me ?
  • you can check bcrypt document for creating a hashed password, then from an array that contains 300 users loop through assign each user one new hashed password and do insertmany operation