2017-03-01 78 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

相关问题