亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Mongoose 在架構級別排除文檔結果

Mongoose 在架構級別排除文檔結果

慕婉清6462132 2023-09-07 16:36:13
我正在為我的用戶創建一個模型。每個用戶都有一個isVerified布爾值屬性。我希望能夠調用Model.find貓鼬模型并排除所有文檔,而isVerified === false不必在查詢期間指定這一點。我想在架構中設置它,以便每當Model.find調用時這些文檔都會自動排除。任何幫助表示贊賞用戶模型:const UserSchema:Schema = new Schema({    name: {        type: String,        required: true,        trim: true,    },    email: {        type: String,        required: true,        unique: true,        lowercase: true,        trim: true,        index: true,        validate: {            validator: (value:string) => validator.isEmail(value),            message: (props:any) => "Invalid Email Address"        },    },    password: {        type: String,        trim: true,        required: true,        select: false,        minlength: 6,        validate: {            validator: (value:string) => !validator.contains(value, "password"),            message: (props:any) => "Your password cannot contain the word 'password'"        }    },    phoneNumber: {        type: String,        trim: true,        required: true,        validate: {            validator: (value:string) => validator.isMobilePhone(value, 'any', {strictMode: true}),            message: (props:any) => "Please include country code (e.g. +233 for Ghana +44 for the United Kingdom) to phone number"        }    },    isActive: {        type: Boolean,        default: false    }    ,    tokens: [        {            token: {                type: String,                required: true            }        }    ]},{    strict: "throw",    timestamps: true})編輯:我做了一些挖掘,看來我可以覆蓋基本方法來重新實現返回的查詢。我嘗試這樣做,如下所示:UserSchema.statics.find = function () {    let query = UserModel.find.apply(this, arguments);    query.where('isActive').ne(false)    return query;}但是我收到以下錯誤 RangeError: Maximum call stack size exceeded
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

終于想通了。需要將更改應用于Model對象而不是實例,UserModel如下所示:


UserSchema.statics.find = function () {

    let query = Model.find.apply(this, arguments);

    query.where('isActive').ne(false)

    return query;

}

現在 find 方法會跳過不活動的用戶


查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號