如何在猫鼬中同时增加一个从 00000 开始的 5 位计数器

How to concurrently increment a 5 digit counter starting with 00000 in mongoose

我想创建一个登录号。 mongodb 中的字段,从 00000(5 位数字)开始,然后每次添加一本书时将 5 位数字递增 1。

这将取决于用例,但我建议只使用数据库中的普通数字,然后在将其返回到需要填充数字的位置之前,将其填充到想要的数字。

这也可以通过 mongoose 中的 set & get 选项轻松完成。
至于自增,我建议使用mongoose-auto-increment or @typegoose/auto-increment。 (选择最适合您的需求)

PS:如果不需要这种方法,唯一的其他选择是将该数字作为字符串或缓冲区存储在数据库中,这将是低效的。