MongoDB自增ID如何实现长尾词自动生成?
- 内容介绍
- 文章标签
- 相关推荐
本文共计91个文字,预计阅读时间需要1分钟。
javascriptconst 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');
Model.jsconst 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分钟。
javascriptconst 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');
Model.jsconst 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; });

