2016-05-17 90 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