Auto Increment Value In a SimpleSchema


#1

Hello,

I’m trying to have a collection with a JobNumber value that increments everytime a job is added to the collection.

I realize that the way to go is by autoValue in the schema and by using konecty:mongo-counter package. But I have been trying with several failed attempts.
Is there a simple example out there that uses this package?

Thank you very much for your help.

Edit:
So I did this, which initially works, but I’m not sure how effective it is in the long run.

		autoValue: function(){
			if ( this.isInsert ){ 
   				 var LastjobNUm = Recipes.find().count();
   				 var jobNum = LastjobNUm++;
   				 return jobNum; 

   				} else if ( this.isSet ){
   					 this.unset(); 
   					}
   				}

#2

I stayed away from doing this in autoValue for performance and de-coupling reasons.

IMO I’ll put validation logic in the schema but auto-increment is a DB thing so I like that separate - that way I can use the schema whenever I want for data validation purposes without worrying about peripheral data access occurring.

In my SQL Server days I used auto-increment all over the place like you’re doing here but in Meteor I don’t.

That said I do all CRUD in methods - so when I am inserting a record that requires an auto-incremented value (I use the mongo-counter package too) I just do that as a pre-step to the insert passing in the result of mongo-counter to the document in the insert operation.


#3

https://atmospherejs.com/konecty/mongo-counter


#4

I had a look at the package and tried to implement it, but the tutorial is very vague and I can’t seem to get it right. I would love a simple example.


#5

I completely agree with you. The app that I am developing will be for a limited user capacity (5-10), would coupling still happen?
Also, could you point me to an example that follows Mongo Counter?

Thank you for your help.


#6

@lzh93 not really sure where you got stuck. can you be more specific about what you tried and what fails?

here’s some random code related to incrementCounter

Tinytest.add('mongo-counter', function(test){
	Package.deleteCounters();
	test.equal( AtomicCounter.increment('foo'), 1 )
	test.equal( AtomicCounter.increment('foo'), 2 )
	test.equal( AtomicCounter.increment('foo'), 3 )
	test.equal( AtomicCounter.increment('foo', 10), 13 )
	test.equal( AtomicCounter.decrement('foo'), 12 )
	test.equal( AtomicCounter.decrement('foo', 10), 2 )
	AtomicCounter.set( 'foo', 100 )
	test.equal( AtomicCounter.increment('foo'), 101 )
	test.equal( AtomicCounter.increment('bar'), 1 )
});
function sequentialString(prepend, idType) {
  if (process.env.NODE_ENV !== "production") {
    //Make ids more random during development to prevent saasu duplicate invoice# problems
    return prepend + "-DEV-" + Random.id(6).toUpperCase();
  } else {
    // Sequence number, atomic increment
    // Init number is 1001
    // NOTE: `incrementCounter` is defined by the `konecty:mongo-counter`
    var digitString = incrementCounter(AtomicCounters, idType, 1) + 1000;

    return prepend + digitString;
  }
}
Counters = new Mongo.Collection('counters');

Counters.generateRemoteIdString = function(remoteId, idType) {
  switch(idType) {
    case  'helpScout':
      return "HS" + remoteId;
      break;
    default:
      return null;
      break;
  }
}

Counters.generateIdString = function(idType, parcelType) {
  // Initial character
  var idString;
  switch(idType) {
    case  'lineItem':
      idString = 'M';
      break;
    case 'purchaseId':
      idString = 'P';
      break;
    case 'userId':
      idString = 'C';
      break;
    case 'orderId':
      idString = 'S';
      break;
    case 'shippingOrderId':
      idString = 'SX';
      break;
    case 'consolidationRequest':
      idString = "CR";
      break;
    case 'parcelId':
      idString = (function() { // TODO XXX
        if ( parcelType === Parcel.type.BUYING_SERVICE ) return 'PP';
        else if ( parcelType === Parcel.type.FORWARDING_SERVICE ) return 'PF';
        else return 'PX';
      })();
      break;
    default:
      // some default
      idString = 'X';
      break;
  }

  // Sequence number, atomic increment
  // Init number is 1001
  var digitString = incrementCounter(Counters, idType) + 1000;
  
  idString += digitString;

  return idString;
};