вы можете определить свои запросы как функции.Предположим ниже схему постов в блоге, которую вы можете сохранить как любимую.Если вам нужно увеличить или уменьшить favCount
, вы можете выполнить это, как показано ниже,
const PostSchema = new Schema(
{
//other necessary fields as title, description of the post..etc
favoriteCount: {
type: Number,
default: 0,
},
},
{ timestamps: true },
);
PostSchema.statics = {
incFavoriteCount(postId) {
return this.findByIdAndUpdate(postId, { $inc: { favoriteCount: 1 } });
},
decFavoriteCount(postId) {
return this.findByIdAndUpdate(postId, { $inc: { favoriteCount: -1 } });
}
};
export default mongoose.model('Post', PostSchema);