Update (increment) value when Parse Object is retrieved? - parse-platform

Is there a way to update a Parse Object field (increment) before it is returned from a Parse Query or retrieve method?
An example is for a view count to be incremented every time it is returned.
I've looked at beforeFind method but it looks like it is just for modifying queries before it is executed.

Add a cloud function to handle the query instead of calling the query directly from the client. In the find() success handler, iterate through and increment that value, call a Parse.Object.saveAll on the results, and then return all the results in the response.success() call.

Currently there is no such way that modifies the values you retrieve from queries. You can only edit the query statement. If there is a need to modify the value into DB, you have to retrieve first, update the table, then return back the incremented value.

Related

DynamoDB Query Returns Incomplete Data

Why does my query in Lambda not return everything between the two Timestamps? It consistently returns the same, incomplete data.
In DynamoDB Item Explorer, I can query recent Timestamps and find the appropriate items by Device.
When I query in my Lambda, much of that same data is missing.
var params = {
Statement : `SELECT * FROM temps WHERE "Timestamp" >= ${fromParam} AND "Timestamp" <= ${toParam}`,
}
dynamodb.executeStatement(params, function(err, data) { ...
My DynamoDB table looks like this:
DDB will only read (note read, not the same as return) 1MB of data at a time.
If you are doing any filtering, then the returned data will be less than the 1MB read.
If there's more data to read, DDB will include LastEvaluatedKey in it's response. You'll need to call Query() again passing in the returned LastEvaluatedKey as ExclusiveStartKey
Thus, unless you can guarantee you'll never have more than 1MB of data to read, you'll want to call Query() in a loop till you get back all the data.
EDIT
Yes, if nextToken is returned, you'll need to pass that back in the next call..
I've never used execute statement, but it appears you're doing a full table scan. Rather than a query. You need to include a where device = in order to use query behind the scenes.
If you really need records for all devices, consider a adding a GSI with a single value as the partition key and timestamp as the sort key. Then use FROM TEMPS.mytsidx

Search in GET with a multifield condition

I would like to perform an elasticsearch query relying on a GET request.
This query successfully allow me to see all the messages inside the index addressed to a particular sender (i.e., where sender.id == some value).
http://localhost:9200/myindex/messages/_search?q=sender.id:user1
Now, I would like to add a new field. In my case study, to retrieve only the messages with the boolean flag received set to true. So I tried:
http://localhost:9200/myindex/messages/_search?q=sender.id:user1&received:true
But this doesn't work, and I can't find any documentation / example on how to perform a GET query with multifield.
Please note that the parameter received exists, is always set, and is correctly working when used alone.
The q parameter take the lucene query syntax.
So to add another condition use the following:
http://localhost:9200/myindex/messages/_search?q=sender.id:user1%20AND%20received:true

Is there a way to limit the keys returned on an object in a query?

I have a user object with dozens of keys on it. Is it possible to give a query some parameters such that only certain fields on the user come back and I have to fetch the rest?
To limit the keys in the returned result set you can use 'keys' parameter in rest api.
Sample GET request:
https://demodomain/parse/classes/ClassName?keys=key1,key2,key3
For more:
https://docs.parseplatform.org/rest/guide/#query-constraints

mgo - reduce update and find to one query only

I find myself having to write two db queries and I would like to know whether it's possible to reduce to one.
I am updating a document and then finding the document again in the database. Is it possible to update and receive the full updated document in the response of the update query? This would save a call to the db.
I should explain that the document I'm saving won't be a complete document which is why I'm having to retrieve it from the database to get all the fields.
Yes, it is possible. Please have a look at the documentation of the Query.Apply method, which runs the findAndModify MongoDB command.
Straight from the documentation, this example increments a counter and prints its new value:
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
info, err = col.Find(M{"_id": id}).Apply(change, &doc)
fmt.Println(doc.N)

How to filter and delete a record that has a specific attribute

Im trying filter records with that has a specific value key and delete them. I tried "withFields" and "hasFields" but seems that i can't apply delete to them. My question is how can i do that?
r.db('databaseFoo').table('checkpoints').filter(function (user) {
return user('type').default(false);
}).delete();
If you want all documents that have a type key, you can use hasFields for that.
r.db('databaseFoo').table('checkpoints')
.hasFields('type')
In your current query, what you are doing is getting all documents that don't have a type key or where the value for type is equal to false. This might be what you want, but it's a little confusing if you only want documents that have a type property.
Keeping a reference to the original document
The problem with using hasFields is that it converts a selection (a sequence with a reference to the specific rows in the database) that you can update, and delete into a sequence, with which you can't do that. This is a known issue in RethinkDB. You can read this blog post to understand the different types in ReQL a bit better.
In order to get around this, you can use the hasFields method with the filter method.
r.db('databaseFoo').table('checkpoints')
.filter(r.row.hasFields('type'))
.delete()
This query will work since it returns a selection which can then be passed into delete.
If you want to get all records with with a specific value at a specific key, you can do so a couple of different ways. To get all documents where the property type is equal to false, you can do as follows:
r.db('databaseFoo').table('checkpoints')
.filter({ type: false })
or, you can do:
r.db('databaseFoo').table('checkpoints')
.filter(r.row('type').eq(false))

Resources