Ind*_*ial 5 mongoose node.js express
mongoose
我的node.js
应用程序中有一个模型,代表发票.我已经想出了大部分内容,但我确实需要确保我的发票被计算/递增以便能够为我的客户提供适当的参考.
使用SQL数据库,我会创建一个包含AUTO-INCREMENT
此值的列,但这显然没有内置到MongoDB中.那么我将如何实现这一目标mongoose
呢?
以下是我的模型现在的样子:
var InvoiceSchema = new Schema({
reference: {type: Number, default: 0}, // The property I want to auto-incr.
dates: {
created: {type: Date, default: Date.now},
expire: {type: Date, default: expiryDate()}
},
amount: {type: Number, default: 0}
// And so on
});
Run Code Online (Sandbox Code Playgroud)
kei*_*ics 26
Controller.findByIdAndUpdate(ID_HERE, {$inc: {next:1}}, function (err, data) {
});
Run Code Online (Sandbox Code Playgroud)
// next是字段,输入:Number
一般来说,在 MongoDB 中,_id(或其他字段)不会使用自动增量模式,因为这在大型数据库集群上不能很好地扩展。相反,人们通常使用对象 ID。
有关更多信息,请查看此链接:http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
因此,最重要的是,您可以只使用对象 ID,它们是唯一的。
归档时间: |
|
查看次数: |
16310 次 |
最近记录: |