Basic question on MAPFUNCTION

33 views
Skip to first unread message

Sandra

unread,
Mar 28, 2014, 4:04:43 PM3/28/14
to mobile-c...@googlegroups.com
Hello,
    I have several documents and I classify them by "type" for instance, let's say we want to retrieve only documents related to users, type=user.
    How do I query by type?
   My MAPFUNCION looks like this but it seems not to be working as it doesn't retrieve any row.


- (void)usersCBView {

    

    [[self.database viewNamed: @"users"] setMapBlock: MAPBLOCK({

        id DocType = [doc objectForKey: @"type"];

        if (DocType == (id) @"user")

            emit(@[doc[@"id"], doc[@"pwd"], doc[@"usercategory"], doc[@"name"]], nil);

    }) version: @"1.1"];

    

}


Any help is very well appreciated,

BR

Sandra

Jens Alfke

unread,
Mar 28, 2014, 5:28:14 PM3/28/14
to mobile-c...@googlegroups.com

On Mar 28, 2014, at 1:04 PM, Sandra <txm...@gmail.com> wrote:

> if (DocType == (id) @"user")

You can’t compare NSStrings that way. Since Objective-C is based on C, the “==“ operator compares pointers, not object values. To compare the two strings for equality you’d use
if ([DocType isEqualToString: @“user”])

—Jens

Sandra

unread,
Mar 28, 2014, 7:13:44 PM3/28/14
to mobile-c...@googlegroups.com
Thanks a lot Jens, that will make the trick.
BR
Sandra

Sandra

unread,
Mar 31, 2014, 3:03:27 PM3/31/14
to mobile-c...@googlegroups.com
Hello Jens,
    One more question: I need to sort the query result by "id" where id is an integer not an string. So I need the users to be sorted as 1, 2 ,3 4 etc but instead I'm getting  1, 11, 12, 13...2,20,21.
    How do I tell couch base to sort by integer id? do I have to sort them manually?
Thank you,
Sandra




On Friday, March 28, 2014 2:04:43 PM UTC-6, Sandra wrote:

Jens Alfke

unread,
Mar 31, 2014, 4:30:55 PM3/31/14
to mobile-c...@googlegroups.com

On Mar 31, 2014, at 12:03 PM, Sandra <txm...@gmail.com> wrote:

    How do I tell couch base to sort by integer id? do I have to sort them manually?

Emit the key as a number, not a string.

In the map block you showed below, if doc[@"id”] is a numeric string, you should consider storing it as a number instead; it’s more efficient. If for compatibility it has to be a string, you can convert it to an NSNumber like
NSString* idStr = doc[@"id”];
NSNumber* idNum = @(idStr.integerValue);
Then you can emit idNum.

—Jens

Sandra

unread,
Mar 31, 2014, 7:04:08 PM3/31/14
to mobile-c...@googlegroups.com
Awesome! thanks a lot!


On Friday, March 28, 2014 2:04:43 PM UTC-6, Sandra wrote:
Reply all
Reply to author
Forward
0 new messages