Hello. Everyone i didn’t found anything aboute my request so i’ve decided to leave a question here.
I’ve a bucket “settings” with 3 types of documents they id’s starts with prefixes “users…”, “places…” and “prices”. How can i find a latest documet with id “users_22.7.2020” of started with another prefixes.
SELECT MAX([d.timestamp,d])[1]
FROM Settings d
WHERE META().id LIKE “users_%”
but in couchbase web interface it says that:
“This query contains the following fields not found in the inferred schema for their bucket,
the field names might be misspelled:
Settings.timestamp”
So the question is the timestamp in document metadata or somewhere else?
It doesn’t appears in metadata, i can’t see it.
I want the newest document from base. But right now i don’t have a field with timestamp. So i see i need to create it first, the couchbase doesn’t store the time document created.
By the way this code does’nt return any document content:
var Users_settings = SettingsBucket.GetDocument<dynamic>("SELECT MAX([d.timestamp,d])[1] FROM Settings d WHERE META().id LIKE ''users_%''");
Console.WriteLine(Users_settings.Content);
It returns empty string. But when i enter this query in web interface it returns document content.
var Users_settings =
SettingsBucket.GetDocument<dynamic>("SELECT MAX([d.timestamp,d])[1].* FROM Settings d WHERE META(d).id LIKE 'users_%'");
var Users_settings =
SettingsBucket.GetDocument<dynamic>("SELECT MAX([d.timestamp,META(d).id])[1] AS id FROM Settings d WHERE META(d).id LIKE 'users_%'");
Try above, if still have issue, that might be how GetDocument called. may be u need document id. cc @deniswsrosa
It possible , I am not expert on SDK some one has to answer that.
If your document key is of “users_22.7.2020” want latest based on latest date in document key you can also try some thing like this, (LET expression doing date in YYYY-MM-DD format, if you have hours/minute/seconds you need to expand or change document key format)
SELECT MAX([date, META(s).id])[1]
FROM Settings AS s
LET date = CONCAT(SPLIT(SUBSTR(META(s).id,6),".")[2],"-",CASE WHEN LENGTH(SPLIT(SUBSTR(META(s).id,6),".")[1]) = 1 THEN "0" ELSE "" END, SPLIT(SUBSTR(META(s).id,6),".")[1],"-",CASE WHEN LENGTH(SPLIT(SUBSTR(META(s).id,6),".")[0]) = 1 THEN "0" ELSE "" END, SPLIT(SUBSTR(META(s).id,6),".")[0])
WHERE META(s).id LIKE "users_%";