Dynamoose: how to get the latest rows? - node.js

I'm trying to get the latest rows of a table, using Dynamoose.
I read about query().ascending() and query.descending(), but I need to query the whole table, which requires the hashkey to be empty, as far as I understood. scan() doesn't support sorting.
Something like:
MyModel
.scan() // 1. scan the whole table
.descending('my_date') // 2. sort by descending by a date
.limit(100) // 3. limit the results to 100
.exec(function(error, data) {
// return error or data
});
Does anyone know how to get the latest rows?
Thanks in advance!

Neither DynamoDB or Dynamoose API support to get the latest results. You may need to perform this at client side (i.e. write custom code to achieve this result).
DynamoDB API:-
The attribute needs to be defined as SORT key to sort the data by ascending or descending.
Even Dynamoose API doesn't sort the data by all attributes. It sorts the data by sort key only.
query.descending() - Sorts the data by sort key attribute of the table in descending order

Related

Microsoft Graph API $filter=name eq 'foo' query not working on GET workbook/tables/{id}/columns. No error and no filtering

I'm looking at a table (Table1) inside an Excel book saved on my OneDrive for Business account. I then want to get the maximum value in the CREATEDDATE column from this table.
I want to avoid pulling down the whole table with the API, so I'm trying to filter the results of my query to only the CREATEDDATE column. However, the column results from the table are not being filtered to the one column and I'm not getting an error to help troubleshoot why. All I get is an HTTP 200 response and the full unfiltered table results.
Is it possible to filter the columns retrieved from the API by the column name? The documentation made me think so.
I've confirmed that /columns?$select=name works correctly and returns just the name field, so I know that it recognizes this as an entity. $filter and $orderby do nothing when referencing any of the entities from the response (name, id, index, values). I know that I can limit columns by position, but I'd rather explicitly reference the column by name in case the order changes.
I'm using this query:
/v1.0/me/drive/items/{ID}/workbook/tables/Table1/columns?$filter=name eq 'CREATEDDATE'`
You don't need to $filter here, just pull it by the name directly. The prototypes from the Get TableColumn documentation are:
GET /workbook/tables/{id|name}/columns/{id|name}
GET /workbook/worksheets/{id|name}/tables/{id|name}/columns/{id|name}
So in your case, you should be able to simply call call:
/v1.0/me/drive/items/{ID}//workbook/tables/Table1/columns/CREATEDDATE

How to get last inserted 10 records in descending order using dynamodb

I am new in amazone-dynamodb. I want last inserted 10 records in descending order using dynamodb.
DynamoDB allows to sort the data only by sort key attribute. The ScanIndexForward option can be used to sort the data in ascending or descending order.
Please note that the ordering will be done for the specific partition key only. It will not sort all the items in the table and give you the last 10 records. The sort operation can be done for the specific partition key.
ScanIndexForward
Specifies the order for index traversal: If true (default), the
traversal is performed in ascending order; if false, the traversal is
performed in descending order.
Sort key definition and example:-
A composite partition-sort key is indexed as a partition key element
and a sort key element. This multi-part key maintains a hierarchy
between the first and second element values. For example, a composite
partition-sort key could be a combination of “UserID” (partition) and
“Timestamp” (sort). Holding the partition key element constant, you
can search across the sort key element to retrieve items. This would
allow you to use the Query API to, for example, retrieve all items for
a single UserID across a range of timestamps.
Sounds like you are using the DynamoDB example here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.01.html
The sample data does not have insertion timestamps.
Another catch is, that you can only sort at DynamoDB by using the Sort Key, otherwise you need to perform the sorting in code.
So if your Partition Key is the Year, and the Sort Key is the Title, you need to:
Introduce an attribute which provides you with a timestamp of creation.
Create the table with an LSI of this attribute, or create a GSI using the new attribute as your Sort Key.
Now you can use query!
The Query API has an option to:
Sort by the Sort Key in descending order (using ScanIndexForward parameter)
Limiting the number of items returned (using Limit parameter)
The answer by Abhaya Chauhan is mostly correct, though there is one inaccuracy. The Limit parameter does not actually limit the number of items returned, but rather limit the number of items scanned (irregardless of whether they match the search criteria).
Thus if you set a Limit of 10, you might get anywhere between 0 and 10 items. See the below docs for more info:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Limit

How to retrieve item closest to another item in DynamoDB?

I have a dynamo DB table where the sort key has a numeric value.
I have a requirement to retrieve the first item which has a lower value than the one, that I have.
I have gone through http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#API_UpdateItem_Examples docs but I can see no way to:
- sort the output
- limit the result to 1 entry
Is there any way to actually achieve what I want with dynamo DB?
EDIT:
According to this: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
The results are sorted using sorting key, and when it's numeric, they are sorted descending. Which is great, but I still can't find any way to get only a single result [don't want to "pay" for the full table scan in some cases].
Are you searching for the next item which has a lower sort key within the same Partition Key?
In that case, you are able to use Query as you've found, sort in Descending and Limit to 1. This will not scan the entire table.
Alternatively, if you wish you scan cross Partitions, unfortunately a Table Scan is the only way to do this.

Insert docs in sorted order on mongodb

In mongodb, I want to insert the data in sorted order based on some field.
The way I am doing, before insertion compare the data with data which is in collection and then insert it on that particular position. Is the insertion at particular position is possible in mongodb using node.js
You can't insert a doc at a specific spot in the collection. Even if you could, it wouldn't matter because you can't rely on the natural order of MongoDB documents staying consistent as they can move over time as the docs in a collection are updated.
Instead, create an index on the field(s) you need your docs sorted on and then include a sort clause in your queries to efficiently retrieve the docs in that order.
Example in the shell:
// Create the index (do this once)
db.test.ensureIndex({someField: 1})
// Sorted query
db.test.find().sort({someField: 1})

How to get multiple keys in couchdb sorting by date

I have a view which emits values as
function(doc) {
var d = new Date(doc.created_date);
emit([d,doc.unique_id],doc);
}
Here i have to get multiple unique_id's in one query. so i use Keys=["unique_id1","unique_id1"] to get the id's.. but i need them to be sorted by date and also get the unique_id's. If i query as mentioned above i am not getting any results from db.but if i change the view as below and query it i am getting results correctly but not sorted by date.
function(doc) {
emit(doc.unique_id,doc);
}
Could anybody suggest me how to get both in a single query????
Select * from db where unique_id in {"1","3"} order by date
This is what i need in couchdb
You can't do that. In CouchDB, you can either query by a unique key or set of keys, or you can query for a range. So, if you want to query by a set of unique ID's without having to specify their creation date in the query, you cannot have the creation date in the key, and so CouchDB can't order by creation date for you. In that case, you could use a unique-ID-only key and sort the results by created_date on the client side (i.e. after querying).
The alternative is that you specify a key including [unique_id, created_date], but in that case you can't query for [1, *] and [3, *] without also including [2, *] (which you could again filter out after querying, on the client side).

Resources