Bryce
Posts: 0
Joined: Thu Oct 03, 2013 6:08 am

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

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

Hi Bryce,

I'll take a look and update.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

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.
Bryce
Posts: 0
Joined: Thu Oct 03, 2013 6:08 am

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

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.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

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

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

Bryce
Posts: 0
Joined: Thu Oct 03, 2013 6:08 am

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

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

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

Hi Bryce,

Appery.io does not support $inc.

Dupdroid
Posts: 0
Joined: Sat Mar 30, 2013 9:07 pm

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

Hi,

Has this been implemented yet?

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

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

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

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.

MobileBen
Posts: 0
Joined: Sat Aug 10, 2013 7:56 pm

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

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.

Return to “Issues”