使用计数器集合在 node-mongodb-native 中自动递增 [英] auto increment in node-mongodb-native using counters collection

查看:41
本文介绍了使用计数器集合在 node-mongodb-native 中自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以实现 计数器集合,如 node-mongodb-native 的文档中所述?

Is there any way of implementing a counters collection as described in the docs with node-mongodb-native?

我试图通过嵌套超过 9000 个回调来避免这样做(恕我直言,这听起来不整洁且不优雅),但编写此本机驱动程序的好人拒绝实现同步/阻塞调用.

I'm trying to avoid doing this by nesting over 9000 callbacks (which IMHO sounds untidy and not-elegant) but the good people who wrote this native driver refused to implement synchronous/blocking calls.

本机驱动程序是否提供某种方式来调用用户定义的函数并在查询期间使用它们的返回值?或者是否有另一种提取顺序计数的方法....也许仅从 ObjectID() 中提取?

Does the native driver provide a someway to call user defined functions and use their return values during queries? Or could there be an alternate way of extracting a sequential count....maybe solely from an ObjectID()?

有什么想法吗?


这不适用于 _id 字段(由 db.coll.save(x) 处理)
我在一个集合中有不同类型的文档.如果你知道我的意思,每一个都需要它自己的类型序列"或类型序列".


This is not for the _id field (That's taken care of by db.coll.save(x))
I have different types of documents in a collection. Each of these needs its own "type serial" or "type sequence" if you know what i mean.

我已经通过一些嵌套调用实现了这个(以及其他东西),如下所示.doc 是来自客户端的 JSON.parse.我必须使用 _id(按 db.coll.save 排序)和 typeserial<将此 doc 返回给客户端/code>(当前按 db.coll.count 排序,如下所示)

I've already implemented this (plus other stuff) with afew nested calls as shown bellow. doc is JSON.parse'd from the client. I have to return this doc to the client with an _id (sorted by db.coll.save) and with a typeserial (currently being sorted by db.coll.count as shown bellow)

 ///more nesting Async calls above. 
 db.collection('dox').count( { "type" : doc.type } ,
    function( err , count )
    {
        ///{some err checking code here}
        doc.typeseq = (1+count);
        db.collection('dox').save( doc ,
            function( err , doc )
            {
                ///{more code here}
                ///Finally return JSON.stringified doc with new info/members to client-side
            }
        );
    }
 );

我只想知道是否有更优雅的反异步方式来获取我的 doc.typeserial

I'd just like to know if there is a more elegant, anti-async way of getting my doc.typeserial

推荐答案

我基本上打算把我的评论作为答案:

I am basically going to slap my comment as an answer:

您只需要两个回调,一个用于获取预定义的原子检索 _id,另一个用于实际插入您的文档.一旦你有你的自动增量 _id,它应该总是以这种方式对你插入的文档是唯一的

You only need two callbacks, one to get a predefined atomically retrieved _id and the other to actually insert your document. Once you have your auto increment _id it should always be unique to your inserted document this way

为了解释更多,当您使用 findAndModify$inc 并从计数器集合返回 _id 然后应该是唯一的运行该脚本以及随之而来的文档插入.这种方法基本上没有竞争条件.

To explain more, when you use findAndModify to $inc and return from the counters collection that _id should then be unique that the running of that script and so the consequental insertion of a document. Basically there would be no race condition in this method.

这确实意味着您将需要某种父函数,例如 insertWithAI,它将执行使用 findAndModify 链接后​​一个插入回调的第一个回调,但在最后当天你应该只需要两次回调.

This does mean you will need some kind of parent function like insertWithAI which will do the first callback of using findAndModify to chain the latter callback of inserting but at the end of the day you should only need two callbacks.

这篇关于使用计数器集合在 node-mongodb-native 中自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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