The Traveling Coder
The Traveling Coder

Reputation: 311

Mongoose find and update all

So currently I am using this

await User.findOneAndUpdate({email: req.user.email }, {lastLoad: 'dfConsignee'});

But I want to be able to update all users with the same truckNumber to be updated when the form is submitted.

SO I tried using this

await User.update({truckNumber: truckNumber }, {lastLoad: 'dfConsignee'});

But this only updated the active user for some reason. How would I go about updating all the accounts with the same truck number on form completion?

Thank you guys in advance <3

Edit ----------------------------------------------------------------------------------------------------------------------------

By active user, I meant logged in user.

Mongoose Schema:

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    },
    isVerified: {
        type: Boolean,
        default: false
    },
    profilePic: {
        type: String,
        default: 'abc'
    },
    truckDriver: {
        type: Boolean,
        default: false
    },
    truckNumber: {
        type: Number
    },
    admin: {
        type: Boolean,
        default: false
    },
    customer: {
        type: Boolean,
        default: false
    },
    loadPlanning: {
        type: Boolean,
        default: false
    },
    lastLoad: {
        type: String,
        default: "dfConsignee"
    }
});

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

module.exports = User;

Sample Document:

{"_id":{"$oid":"aaaaaaaaaaaaaaaa"},
"isVerified":true,
"profilePic":"abc",
"truckDriver":true,
"admin":false,
"customer":false,
"loadPlanning":false,
"lastLoad":"aAtShipper",
"name":"Nicholas Cage",
"email":"[email protected]",
"password":"password",
"date":{"$date":{"$numberLong":"1580858993547"}},"__v":{"$numberInt":"0"},
"truckNumber":"123",}

To add a bit more clarification, when the form is submitted, it will pull the logged in users truck number. When the form is submitted, I am wanting everyone who shares that same truck number to have their value in lastLoad be updated as well.

Upvotes: 1

Views: 7108

Answers (2)

The Traveling Coder
The Traveling Coder

Reputation: 311

OMG Finally figured this out :D

I thought that when I restarted Nginx it would restart the app running on the server ( I would see front end changes doing this, but the back end changes wouldn't show up), once I restarted PM2 all the back end changes came along with it.

Thank you everyone for the help :D

Now "await Users.updateMany({truckNumber: req.user.truckNumber}, {lastLoad: 'aAtShipper'}" will update all accounts with a shared truck Number :D

Upvotes: 1

Sam Forbis
Sam Forbis

Reputation: 179

Try using updateMany(). The resulting code would look something like this:

User.updateMany({truckNumber: truckNumber}, {lastLoad: 'dfConsignee'});

If you really want to familiarize yourself with Mongoose and MongoDB as a whole, I'd suggest taking a look at the API docs for Mongoose and the MongoDB Manual, it goes into much more detail about the tools you have available to you when using MongoDB. That's how I found the answer to this question!

Upvotes: 1

Related Questions