MongoDB自增ID如何实现长尾词自动生成?

2026-04-06 19:101阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计91个文字,预计阅读时间需要1分钟。

MongoDB自增ID如何实现长尾词自动生成?

javascriptconst Mongoose=require('mongoose');const Schema=Mongoose.Schema;

let counter=1;let CountedId={ type: Number, default: ()=> counter++ };

MongoDB自增ID如何实现长尾词自动生成?

const ModelSchema=new Schema({ id: CountedId, // ...});

const Model=Mongoose.model('Model');

Model.js

const Mongoose = require('mongoose'); const Schema = Mongoose.Schema; let counter = 1; let CountedId = {type: Number, default: () => counter++}; const ModelSchema = new Schema({ id: CountedId, // .... }); const Model = Mongoose.model('Model', ModelSchema); module.exports = Model; Model.find({ id: { $gt: 0 } }).sort({ id: -1 }) .then(([first, ...others]) => { if (first) counter = first.id + 1; });

本文共计91个文字,预计阅读时间需要1分钟。

MongoDB自增ID如何实现长尾词自动生成?

javascriptconst Mongoose=require('mongoose');const Schema=Mongoose.Schema;

let counter=1;let CountedId={ type: Number, default: ()=> counter++ };

MongoDB自增ID如何实现长尾词自动生成?

const ModelSchema=new Schema({ id: CountedId, // ...});

const Model=Mongoose.model('Model');

Model.js

const Mongoose = require('mongoose'); const Schema = Mongoose.Schema; let counter = 1; let CountedId = {type: Number, default: () => counter++}; const ModelSchema = new Schema({ id: CountedId, // .... }); const Model = Mongoose.model('Model', ModelSchema); module.exports = Model; Model.find({ id: { $gt: 0 } }).sort({ id: -1 }) .then(([first, ...others]) => { if (first) counter = first.id + 1; });