如何在流星上创建自动增量字段? [英] How can I create an auto increment field on meteor?

查看:62
本文介绍了如何在流星上创建自动增量字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个自动递增字段,但不需要主要ID,它只是为客户支持应用程序的用户提供易于记忆的案例编号.

I need an auto increment field, but not for the primary id, it's only to provide an easy to remember case number to the users of a customers support application.

我发现本文解释了如何在mongodb http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/,但minimongo尚不支持此解决方案.

I found this article that explain how to create an auto increment field on mongodb http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ but this solutions are not supported on minimongo yet.

如何实现此功能?我不在乎解决方案是否由于错误而跳过了一些连续的案例编号,但是案例编号必须是唯一的.

How can I implement this feature? I don't care if the solutions skip some consecutive case numbers due to errors, but the case number must be unique.

是否有另一种方法来生成唯一的和简短的案例编号?我不想给用户这样的案例代码,例如PxyooJWSKcc3sMz6Lc,因为他们必须参考案例编号来解决问题.

Is there another approach to generate unique and short case numbers? I don't want to give to the users a case code like this PxyooJWSKc3sMz6Lc, because they must refer to their issues with the case number.

Minimong不支持findAndModify,所以我不能使用我发布的链接中列出的第一个解决方案.第二种解决方案还需要minimongo上没有的方法.

Minimong doesn't support findAndModify, so I can't use the first solution listed on the link I posted. The second solution also require methods not available on minimongo.

推荐答案

使用 mongo-counter包,可以使用方法incrementCounter(name)创建一个增量器.该实现基于创建一个自动递增序列字段无需访问流星集合即可直接访问数据库.

Using the mongo-counter package, is posible to create an incrementer with the method incrementCounter(name). The implementation is based on Create an Auto-Incrementing Sequence Field accessing directly the database without going through a Meteor Collection.

Meteor.methods({
    'addRecord':function(doc) {
        doc.id = incrementCounter('docId');
        MyCollection.insert(doc);
        return doc.id;
    }
});

更新

Atmosphere上有新的mongo计数器包,可能比我最初的建议要好.

There are new mongo counter packages on Atmosphere, probably better than my initial recommendation.

这篇关于如何在流星上创建自动增量字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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