我正在寻找一种方法来缩短 MongoDB 数据库生成的 ObjectID,并且我知道一些现有的库,例如 'shortid''short-mongo-id'
但是,我的问题是我希望在 mongoose 模式中为集合中存在的每个文档创建一个新的“shortID”字段,并且这个新字段应该是唯一的,理想情况下是附加到文档。 像这样:
var subjectSchema = new Schema({
shortID: {required: true, unique: true},
//must be unique to each document and is same everytime server restarts
Sex: {type: String, enum: enumSex},
Diagnosis: String,
Projects:[projectPerSubjectSchema]
});
'shortid' https://www.npmjs.com/package/shortid库运行良好并且确实生成了唯一的 ID,但是,每次服务器重新启动时它都会生成不同的 ID,这不是我想要的。
我试过的另一个库 'short-mongo-id' https://www.npmjs.com/package/short-mongo-id能够将 ObjectId 转换为唯一 ID 字符串的 chop 版本,但是,我不确定如何在创建模式时使用它。我试过:
ID: {type: String, 'default': shortid((this.ObjectId).valueOf()), unique: true}
尝试使用 this.ObjectId 获取文档的 ObjectId,使用 valueOf() 对其进行字符串化,但终端显示:
TypeError: Cannot read property 'valueOf' of undefined
所有代码都是在 Node.JS 中完成的,我对 NodeJS 和 MongoDB 都很陌生,所以如果我在上面提供的代码中犯了一些重大错误,请纠正我。提前致谢!
最佳答案
shortid 模块应该可以满足您的需要,您只需要将 shortId 保存为普通字符串字段,并确保在保存之前生成新代码,如下所示(在 mongoose 模式定义中):
const mongoose = require('mongoose')
const shortid = require('shortid')
const Schema = mongoose.Schema
const schema = new Schema({
_shortId: {
type: String,
unique: true
},
... // other fields...
})
// This is for checking if the document already have a _shortId,
// so you don't replace it with a new code
schema.pre('save', function (next) {
let doc = this
if (!this._shortId) {
addShortId(doc, next)
}
})
function addShortId (doc, next) {
let newShortId = shortid.generate()
doc.constructor.findOne({_shortId: newShortId}).then((docRes) => {
if (docRes) {
addShortId(doc, next)
} else {
doc._shortId = newCode
next()
}
}, (err) => {
next(err)
})
}
如果你想为所有现有文档插入一个 _showId,你只需要一个 forEach
并再次保存模型,这就是我正在做的(使用 https://github.com/JMPerez/promise-throttle ,因为它是一个巨大的集合,一次调用 save() 会降低服务器速度):
XXXXX.find({ '_shortId': { '$exists': false } }).then((data) => {
var promiseThrottle = new PromiseThrottle({
requestsPerSecond: 50,
promiseImplementation: Promise
})
var i = 1
data.forEach(element => {
var myFunction = function (j) {
return element.save()
}
promiseThrottle.add(myFunction.bind(this, i++)).then(ss => {
console.log('done ' + ss._shortId)
})
})
关于javascript - 如何缩短 MongoDB ObjectId 并在 Mongoose 模式中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284635/