2016-05-17 89 views
2

我正在寻找一种方法来缩短MongoDB数据库生成的ObjectID,并且我知道一些现有的库如'shortid''short-mongo-id '如何缩短MongoDB ObjectId并在Mongoose模式中使用它

但是,我的问题是我希望为集合中存在的每个文档在mongoose模式中创建一个新的'shortID'字段,并且此新字段应该是唯一的,理想情况下是附加ObjectId的截断版本到文件。 像这样:

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] 
}); 

的“短ID” https://www.npmjs.com/package/shortid库工作很好,确实产生唯一的ID,但是,它会产生不同的ID,每次服务器重启,这不是我想要的。

我试过'short-mongo-id'https://www.npmjs.com/package/short-mongo-id的另一个库能够将ObjectId转换为唯一ID字符串的截断版本,但是,我不确定如何在创建模式时使用它。我已经试过:

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都很新,所以如果我在上面提供的代码中犯了一些重大错误,请纠正我的错误。提前致谢!

+0

您将需要实现[自定义类型](http://mongoosejs.com/docs/customschematypes.html)。但是退一步说:为什么你不能使用普通的'ObjectId'? – robertklep

+0

我认为它会使查询特定对象的信息变得困难,因为用户需要输入长ID,并且URL看起来非常混乱。我可能是错的,如果有更好的方法基于ObjectId轻松地进行查询,请让我知道 –

+0

也许有一种方法不必麻烦用户使用id的吗?即使他们很短,任何形式的身份证通常都不适合普通人需要处理。 – robertklep

回答

1

的短ID模块应该为你所需要的工作,你只需要保存短ID为正常的字符串字段,并保存,这样的(猫鼬模式定义内)之前确保产生一个新的代码:

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) 
    }) 
}) 

此代码的一部分改编自:https://github.com/dylang/shortid/issues/65