Unable to use $inc to update and increement prorperty in mongo db?

I have a collection which has two property id and sequence_value. I am using it for auto-increment now if I want to increase its value then I am not able to do. I get the below error.

enter image description here

Below is my model.

import mongoose, { Schema, Document, Model } from 'mongoose';

export interface ICounterSchema extends Document {
    sequence_value: Number;
    _id: string


var CounterSchema = new Schema({
    _id: {type: String, required: true},
    sequence_value: { type: Number, default: 101 ,required:true}

export const CounterModel = mongoose.model<ICounterSchema>('counters', CounterSchema);

My code which update the value

 export const getNextSequenceValue = async (sequenceName:string) => {
    var sequenceDocument = await CounterModel.findOneAndUpdate({_id: sequenceName },
        {new: true}, 
    return sequenceDocument;

Read more here: https://stackoverflow.com/questions/64940641/unable-to-use-inc-to-update-and-increement-prorperty-in-mongo-db

Content Attribution

This content was originally published by TechChain at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: