Page 1 of 2

How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Tue Nov 26, 2013 6:00 am
by Bryce

Hello,

I need to increment a value in a collection by 1 each time a particular action occurs.

I initially thought I would retrieve the current value from the database, store it in local storage, then use an update service to increment the value back in the collection.

However, the problem with this is the potential delay between getting the current value, and then updating it with the new value (ie. increment by 1), especially when there may be multiple users all updating the same record at the same time.

I did some research, and found that mongoDB has the perfect function for this which increments a specified field in a collection by a specified amount.

It is: $inc

http://docs.mongodb.org/manual/refere...

Unfortunately I have not been able to figure out how to implement this function in my Appery backend database objects.

Can you please tell me how I can use this operator on my Appery backend objects?

Thanks.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Tue Nov 26, 2013 6:53 am
by Kateryna Grynko

Hi Bryce,

I'll take a look and update.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Tue Nov 26, 2013 10:08 am
by Kateryna Grynko

Bryce,

For now, Appery.io doesn't support $inc. As a workaround, create 2 services:

  1. Reads the field value, saves it to a localStorage variable. In mapping, add the following code:prereturn parseInt(value) + 1;/pre2. Updates the field in Database.

How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Tue Nov 26, 2013 10:39 pm
by Bryce

Hi Katya,

Thanks for the prompt, although disappointing, response.

It is really annoying that, although Appery's integrated backend database is "mongoDB", it does not support many of the features that make mongoDB so great.

Two native mongoDB operators that I've been told by Appery support staff that are not supported by the Appery backend database are:

  • geoNear

  • $inc

    This is very disappointing!

    As I explained in my original question, the workaround you suggest to increment a value is not suitable for me, since there is a high chance of inaccuracy when multiple user are updating the same record at the same time due to the potential delay between retrieving the current value and then updating it with the incremented value.

    Can you explain to me how I can use the $inc operator in an Appery REST query on an external mongoDB? I have seen how this can be constructed in a cURL command, however I do not know how to directly translate PUT and POST cURL commands into the Appery REST query builder UI.

    Thanks.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Wed Nov 27, 2013 6:57 pm
by Maryna Brodina

Hello! Can you show us cURL command you need to implement with services?


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Wed Nov 27, 2013 10:28 pm
by Bryce

I believe it would look like this: (note the "$inc" operator in the -d line...)

precurl -X PUT \
-H "X-Appery-Database-Id: {database_id}" \
[-H "X-Appery-Session-Token: {session_token}"] \
-H "Content-Type: application/json" \
-d '{"$inc" : {"count" : 1}}' \
https://api.appery.io/rest/1/db/collections/Notes/{object_id}
/pre
"count" is the field I want to increment by 1.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Thu Nov 28, 2013 1:56 pm
by Kateryna Grynko

Hi Bryce,

Appery.io does not support $inc.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Sat Feb 15, 2014 3:43 pm
by Dupdroid

Hi,

Has this been implemented yet?

I need to add an Auto-Incrementing Sequence Field to a collection.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Sat Feb 15, 2014 4:39 pm
by Alena Prykhodko

Hello Christo, this function is not supported. Not sure it could be implemented from our side. You can implement it yourself. This should be possible with Server code (http://docs.appery.io/documentation/b...), but it requires strong programming skills.


How can I increment a value in a collection using mongoDB's $inc command ?

Posted: Sun May 18, 2014 2:49 pm
by MobileBen

We need access to MongoDB capabilities such as $set, $inc, $geoNear and Aggregation. Server side functions that leverage the database are essential for mobile apps. It is unacceptable to tell developers to pull all of the data to the app, update it and sent it back to the server. It is equally unacceptable to tell developers to build inefficient server side code to do what MongoDB does so well.

It doesn't make sense that this could not be done in the Appery.io database environment. We just need to add a little bit of JSON to the UPDATE request!

Can we get an update on whether or not Appery.io ever intends to support these database features and, if so, when.

Thank you.