2013-09-21 54 views
0

是否可以設置什麼字段應該是「ID」字段?我確定我的方案有:jugglingdb - 設置主鍵字段

var Person = app.ormDb.define('person', { 
    id   : { type: String, index: true, default: function() { return uuid.v4(); } }, 
    oAuthID  : { type: String, index: true }, 
    name  : { type: String }, 
    gender  : { type: String }, 
    birth  : { type: Date }, 
    email  : { type: String }, 
    imageID  : { type: String }, 
    favorites : { type: JSON, default: function() { return {cars : [], animals : []}; } }, 
    date  : { type: Date, default: function() { return new Date(); } }, 
    updated  : { type: Date, default: function() { return new Date(); } } 
}); 

和我的定義id場MongoDB中顯示出來,但是當我用jugglingdb查找一個人Person.id返回值是MongoDB的_id的ObjectId值。所以我的id被隱藏。

回答

0

_id在MongoDB中作爲主鍵保留。這將解釋爲什麼它不能改變。只要它是唯一的,您可以將任何值放入此中。希望有所幫助。

jugglingdb-mongodb適配器中目前還有一個open issue。這解決了爲什麼沒有返回id的原因。基本上,如果調用create時存在object.id,則在插入集合之前將其刪除。