1

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

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.

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()?

Any ideas?

EDIT:
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.

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

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

mrmoje
  • 3,714
  • 3
  • 20
  • 18
  • 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 – Sammaye Aug 22 '13 at 07:07

2 Answers2

1

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

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

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.

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.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • I'm currently using `db.coll.count` to get my sequence, but in an async call. Check my edits above for further clarification (my bad on the ambiguity). – mrmoje Aug 22 '13 at 17:09
  • @Moje Ah that makes it a lot more complicated, however, these are not $inc fields so maybe there might be a way to call a `getSequentialId()` function hmmm – Sammaye Aug 22 '13 at 17:15
  • Touche! That's the tricky part now -> Implementing a persistent counters collection with a `getSequentialId(X)` function...Elegantly. – mrmoje Aug 22 '13 at 17:19
  • @Moje I did have an idea but it didn't pan out with async, I'll keep thinking – Sammaye Aug 22 '13 at 17:23
  • anyone found a solution for this issue? – Rafael Apr 06 '14 at 12:19
0

I implemented auto-increment with the "optimistic loop" with the help of async whilst which worked fine:

Creating incrementing numbers with mongoDB

Community
  • 1
  • 1
Kiechlus
  • 1,167
  • 12
  • 21