auto-increment using loopback.js and MongoDB

前端 未结 3 2152
梦毁少年i
梦毁少年i 2021-01-14 17:07

i want to increase mongodb document number automatically using loopback.

I made function in mongo

 function getNextSequence(name) {
   var ret = db.         


        
3条回答
  •  情深已故
    2021-01-14 17:19

    You can do something like in this example for loopback 4

    let last_record = await this.testRepository.findOne({order: ['id DESC']});
    if(last_record) invoice.id = last_record.id+1;
    

    This will generate your model with the property:

    @property({
        type: 'number',
        id: true,
        default: 1,
        generated: false
      })
      id: number;
    

    Hopefully, this helps, please write me if there is any other code. Thanks

提交回复
热议问题