用猫鼬增加价值? [英] Incrementing a value with mongoose?

查看:76
本文介绍了用猫鼬增加价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 node.js 应用程序中有一个 mongoose 模型,代表发票.我已经弄清楚了大部分内容,但是我确实需要确保对发票进行了编号/增加,以便能够为客户提供适当的参考.

I have a mongoose model in my node.js application, representing invoices. I have figured most of it out already, but I really need to ensure that my invoices are numerated/incremented to be able to provide a proper reference to my customer.

使用SQL数据库,我会创建一个 AUTO-INCREMENT 列来保存该值,但这显然不是MongoDB内置的.那我怎么用 mongoose 做到这一点?

Using an SQL database, I would have created an AUTO-INCREMENT column holding this value, but this obviosly isn't built into MongoDB. So how would I accomplish this with mongoose?

这是我的模型现在的样子:

Here's how my model looks right now:

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
});

推荐答案

通常在MongoDB中,不使用自动递增模式_id(或其他字段),因为这在大型数据库集群上无法很好地扩展.相反,通常使用对象ID.

Generally in MongoDB, one does not use an auto-increment pattern for _id's (or other fields), as this does not scale up well on large database clusters. Instead one typically uses Object IDs.

有关更多信息,请查看此链接: http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

For more info checkout this link: http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

因此,最重要的是,您只能使用对象ID,这些ID是唯一的.

So bottom line you can just use Object IDs, those are unique.

这篇关于用猫鼬增加价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆