在Parse数据库中查找最常出现的值

时间:2015-06-04 19:39:06

标签: javascript ios objective-c mongodb parse-platform

我没有看到如何在Parse中查找表中最常出现的值的文档。如何进行此查询(iOS回答首选,但不限于此)

我认为我可以使用NSPredicate进行某种复合查询,但我仍然不确定如何进行此计算。

1 个答案:

答案 0 :(得分:1)

也许这可以帮助你:

NSArray *yourarray = [NSArray arrayWithObjects:@"X", @"Y", @"Z", nil];
NSCountedSet *yourset = [[NSCountedSet alloc] initWithArray:yourarray];

for (id item in yourset) {
    NSLog(@"Count of %@ is %lu", item, (unsigned long)[yourset countForObject:item]);
}

这可能是NSPredicate的例子(没试过,因为我在TextMate中写道):

NSDictionary *dic1 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:99] forKey:@"visitCount"];
NSDictionary *dic2 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:2] forKey:@"visitCount"];
NSDictionary *dic3 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1003] forKey:@"visitCount"];
NSDictionary *dic4 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1005] forKey:@"visitCount"];
NSDictionary *dic5 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:107] forKey:@"visitCount"];
NSArray *array = [NSArray arrayWithObjects:dic1, dic2, dic3, dic4, dic5, nil];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.visitCount == %@.@max.visitCount", array];

NSArray *result = [array filteredArrayUsingPredicate:predicate];
NSLog(@"Max in objects: %@", result);