2017-03-01 80 views
0

我想在_id字段中使用uuid字符串來代替ObjectId。Mongoose uuid insted ObjectId

型號/ user.js的

var uuid = require('node-uuid'); 

var userSchema = mongoose.Schema({ 
    _id: {type: String, default: uuid.v4}, 
    nick: {type: String, unique : true, default: ""}, 
    email: {type: String, default: ""}, 
    pass: {type: String, default: ""}, 
    admin: {type: Boolean, default: false}, 
    created: {type: Date, default: Date.now}, 
    modified: {type: Date, default: Date.now} 
}); 

獨特之處是必要的嗎?

_id: {type: String, unique : true, default: uuid.v4}, 
+0

可能使用http://stackoverflow.com/questions/31900863/mongoose-does-a-custom-id-need-to-be-declared-as-an-index-and-be-unique。 – JohnnyHK

回答

0

UUID的定義是唯一的,所以您不一定需要將_id定義爲模型中唯一。

+0

但是你仍然希望將其標記爲防止程序員錯誤。 – robertklep

+0

By wiki「雖然UUID將被複制的概率不爲零,但是它非常接近零以至於可以忽略不計」,但是mongodb會自動設置_id作爲PRIMARY KEY(NOT NULL + UNIQUE INDEX)http://stackoverflow.com/questions/31900863/mongoose-does-a-custom-id-need-to-be-decla-as-an-index-and-be-unique對不起dup問題 – mcek

相關問題