我正在為我的用戶創建一個模型。每個用戶都有一個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
Mongoose 在架構級別排除文檔結果
慕婉清6462132
2023-09-07 16:36:13