2014-04-07 36 views
3

默認我想要做這樣的事情:定義功能在一個領域中貓鼬

var categorySchema = new Schema({ 
    id: { 
    unique: true, 
    default: function() { 
     //set the last item inserted id + 1 as the current value. 
    } 
    }, 
    name: String 
}); 

這是更多鈔票?

+0

您將如何使用它? – aiapatag

+0

例如我可以使用另一個值作爲參考設置id值而無需干預,每次添加新值時都可以。 –

回答

5
var categorySchema = new Schema({ 
    id  : { 
    type : Number 
    }, 
    name : { 
    type : String 
    } 
}); 

// Define a pre-save method for categorySchema 
categorySchema.pre('save', function(next) { 
    var self = this; 

    // Example of your function where you get the last ID 
    getLastId(function(id){ 
    // Assigning the id property and calling next() to continue 
    self.id = id; 
    next(); 
    }); 
}); 
+0

你可以評論一下... –

+1

是啊,當然,回答評論:) –