簡體   English   中英

Mongoose 查找並更新所有

[英]Mongoose find and update all

所以目前我正在使用這個

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

但是我希望能夠更新所有具有相同 truckNumber 的用戶,以便在提交表單時進行更新。

所以我試着用這個

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

但這只是出於某種原因更新了活躍用戶。 我 go 如何在表格完成時更新所有具有相同卡車號碼的帳戶?

提前謝謝你們<3

編輯 - - - - - - - - - - - - - - - - - - - - - - - - - ---------------------------------------------- --------------------------

活躍用戶是指登錄用戶。

Mongoose 架構:

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;

樣本文件:

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

需要說明的是,當提交表單時,它將提取登錄用戶的卡車號碼。 提交表單后,我希望共享同一卡車號碼的每個人也更新 lastLoad 中的值。

嘗試使用updateMany() 生成的代碼看起來像這樣:

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

如果你真的想從整體上熟悉 Mongoose 和 MongoDB,我建議你看看API 文檔 MongooseMongoDB 手冊,它更詳細地介紹了你在使用時可以使用的工具342983742 8282 . 就這樣我找到了這個問題的答案!

OMG 終於想通了:D

我以為當我重新啟動 Nginx 時,它會重新啟動在服務器上運行的應用程序(我會看到前端更改這樣做,但后端更改不會顯示),一旦我重新啟動 PM2,所有后端更改都隨之而來它。

謝謝大家的幫助:D

現在“await Users.updateMany({truckNumber: req.user.truckNumber}, {lastLoad: 'aAtShipper'}” 將使用共享卡車編號更新所有帳戶:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM