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,
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With