OrderBy Clause not working

#1

Hello,

I am trying to store a Document with a Dictionary, mailID (unique id).For example i have stored 30 mails for the first time in Couchbase Lite.For the second time, when i launch my app, i will sync one more mail . Now i will be having 31 emails in my DB.

Now if i try to retrieve the document, i am able to get 30 emails followed by 31 st email(which synced recently).I would like to retrieve last inserted email first followed by 30 elements.

Below is the code, which i am trying to save the document,

NSString *docId = [NSString stringWithFormat:@"%@", [_dashBoardObj valueForKey:@"unique_id"]];
CBLMutableDocument *doc = [[CBLMutableDocument alloc] initWithID: docId];

[doc setValue:@"task_list" forKey:@"type"];
[doc setValue:_dashBoardObj  forKey:@"obj"];
[doc setString:docId forKey:@"unique_id"];
NSLog(@"%@",[doc valueForKey:@"obj"]);
NSLog(@"%@",[[doc valueForKey:@"obj"]valueForKey:@"fromEmail"]);

NSError *error;
if (![_database saveDocument:doc error:&error]){
    
}else{
    NSLog(@"Saved");
}

Below is the code which i am try to retrieve the mail list,

_listQuery = [CBLQueryBuilder select:@[S_ID,S_NAME,S_OBJ,S_UniqueID] from:[CBLQueryDataSource database:_database] where:[TYPE equalTo:[CBLQueryExpression string:@"task_list"]] groupBy:group having:queryExpression orderBy:@[[CBLQueryOrdering property:@"unique_id"],[CBLQueryOrdering expression:Descending]] limit:[CBLQueryLimit limit:[CBLQueryExpression integer:30] offset:[CBLQueryExpression integer:Data_limit_str]]];

Any help would be appreciated…

Thank you !!

#2

So you want to sort with the “latest” documents first and present them in batches of 30? Well do you have any properties resembling a timestamp? If so then order by those descending. I can’t say anything more because your code snippet is lacking context for several variables inside and seems to be doing a lot more than just what you say (there is no reason to use groupBy or having that I can tell here)

#3

@borrrden,
Thanks for the revert, i am not using any timestamp for now.I will be storing mailID, which will be a unique.I thought of SQL, in SQL we use to retrieve the doc using unique ID’s right , so i am using the same logic here.

For example the first 30 mailID would be storing like this,
1458
1457
1456
1455
1454
1453
1452
1451

etc…

Next time when i sync a new email, say like the mailID would be 1459, it will be stored below 1451 as follows,

1458
1457
1456
1455
1454
1453
1452
1451
1459

Now my issue was, as 1459 was the latest mailID stored in my DB, i would like retrieve it first, followed by all mailID’s…

#4

That seems like a much simpler query than the one you present. All you need is a select and an order by. Just be sure that you add a call to descending so that it will sort highest first. I believe it would look like this:

[CBLQueryBuilder select:@[S_ID,S_NAME,S_OBJ,S_UniqueID] 
from:[CBLQueryDataSource database:_database] 
where:[TYPE equalTo:[CBLQueryExpression string:@"task_list"]]
orderBy:@[[[CBLQueryOrdering property:@"unique_id"] descending]]];
#5

Thank you @borrrden… it worked…

I was using doing below mistake

[CBLQueryBuilder select:@[S_ID,S_NAME,S_OBJ,S_UniqueID]
from:[CBLQueryDataSource database:_database]
where:[TYPE equalTo:[CBLQueryExpression string:@“task_list”]]
orderBy:@[[[CBLQueryOrdering property:@“unique_id”],[CBLQueryOrdering property:@“DESC”]]]];

Anyways , thanks @borrrden :slight_smile: