How to get multiple keys in couchdb sorting by date - couchdb

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).

Related

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.

Dynamoose: how to get the latest rows?

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

Get timestamp of a collection item in CQL / Cassandra

I know I can use writetime() to get the internal timestamp for a column, but is it possible to get the timestamp for a particular item in a collection (like a list)? My understanding is that collection items are internally stored as individual columns, so it seems like they should have individual timestamps.
The individual columns within a collection do indeed have individual timestamps. You can see this by examining the SSTable directly using the sstable2json function.
CREATE TABLE users (
user_id text,
emails set<text>,
first_name text,
last_name text,
PRIMARY KEY ((user_id))
)
INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('frodo', 'Frodo', 'Baggins', {'f#baggins.com', 'baggins#gmail.com'});
UPDATE users
SET emails = emails + {'fb#friendsofmordor.org'} WHERE user_id = 'frodo';
Then the SSTable looks like this:
[
{"key": "66726f646f","columns": [["","",1444170199819000],
["emails","emails:!",1444170199818999,"t",1444170199],
["emails:62616767696e7340676d61696c2e636f6d","",1444170199819000],
["emails:664062616767696e732e636f6d","",1444170199819000],
["emails:666240667269656e64736f666d6f72646f722e6f7267","",1444170213268000],
["first_name","Frodo",1444170199819000],
["last_name","Baggins",1444170199819000]]}
]
You can see the 3 entries in emails corresponding to timestamps 1444170199819000, 1444170199819000 and 1444170213268000.
However it seems like its not possible to return these through CQL. You won't be able to return individual columns from the set, but it would be reasonable to return the timestamps along with the values for all of the entries in the set, however I can't find any documentation on how to do this so it doesn't look like its supported.
Collection elements have their own timestamps, but it is impossible to get those timestamps using CQL. See corresponding Jira ticket to support this functionality: CASSANDRA-8877.

CouchDB view collation sorted by date

I am using a couchDB database.
I can get all documents by category and paginate results with a key like ["category","document_id"]and a query likestartkey=["category","document_id"]&endkey=["category",{}]`
Now I want to sort those results by date to have latest documents first.
I tried a lot of keys such as ["category","date","document_id"]
but nothing works (or I can't get it working).
I would use something like
startkey=["queried_category","queried_date","queried_document_id"]&endkey=["queried_category"]
but ignore the "queried_date" key part (sort but do not take documents where "document_id" > "queried_document_id")
EDIT:
Example :
With a key like :
startkey=["apple","2012-12-27","ZZZ"]&endkey=["apple",{}]&descending=true
I will have (and it is the normal behavior)
"apple","2012-12-27","ABC"
"apple","2012-05-01","EFG"
...
"apple","2012-02-13","ZZZ"
...
But the result set I want should start with
"apple","2012-02-13","ZZZ"
Emit the category and the timestamp (you don't need the document_id):
emit(category, timestamp);
And then filter on the category:
?startkey=[":category"]&endkey=[":category",{}]
You must understand that this is only a sort, so you need the startkey to be before the first row, and the endkey to be after the last row.
Last but not least, don't forget to have a representation for the timestamp that is adequate to the sort.
The problem with pagination with timestamp instead of doc ID is that timestamp is not unique. That's why you will have problem with paging Aurélien's solution.
I would stay with what you tried but use timestamp as the number (standard UNIX milliseconds since 1970). You can reverse the order of single numeric field just by multiplying by -1:
emit(category, -timestamp, doc_id)
This way result sorted lexicographically (ascending) will be ordered according to your needs:
first dates descending,
then document id's ascending.

Resources