Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto increment a property value in Strongloop loopback model

Is there a built-in way to auto increment a model's property value is Strongloop loopback ? This model has a property named orderNumber and I want it to start at 1 and increment by 1 every time a new model is created. This model is persisted to a mongo DB. If Strongloop loopback does not have a built in way, then what would be considered best practice using javaScript, Node and mongoDB ?

Thanks,

like image 833
Warren Avatar asked Jan 02 '15 16:01

Warren


1 Answers

Ok, This solution works but I am bypassing Loopback and the "mongoDB connector". Here is what I am doing.

Given a model named Sequence that looks like this:

{
    "_id": {
        "$oid": "54ab3ec0cc074e24144f26d7"
    },
    "collection": "SpecialOrder",
    "value": 113
}

I am doing this:

mongoConnector = app.dataSources.MyMongoDBConnectorName.connector;

mongoConnector.collection("Sequence").findAndModify({collection: 'SpecialOrder'}, [['_id','asc']], {$inc: { value: 1 }}, {new: true}, function(err, sequence) {
        if(err) {
            console.log(err.message);
        } else {
            // Do what I need to do with new incremented value sequence.value
        }
    });

Seems like there should be a built in LoopbackJS way of doing this. A lot to go through just to increment a value.

Thanks,

Warren Bell

like image 187
Warren Avatar answered Oct 21 '22 09:10

Warren