查询

数据类型对应查询操作符表

数据类型 可使用的查询操作 说明
String =, in, notIn, !=, isNull, isNotNull, contains, matches, exists, notExists
Number =, >, >=, <, <=, !=, in, notIn, isNull, isNotNull, exists, notExists
array =, in, notIn, isNull, isNotNull, arrayContains, exists, notExists file、object、date 类型的 array 不支持查询操作。如果进行查询,会返回空数组
Boolean =, exists, notExists, isNull, isNotNull
Calendar =, >, >=, <, <=, exists, notExists, isNull, isNotNull
CloudFile isNull, isNotNull, exists, notExists
Object =, hasKey, isNull, isNotNull, exists, notExists
pointer =, in, notIn, !=, isNull, isNotNull, exists, notExists

info file、object、date 类型的 array 不支持查询操作。如果进行查询,会返回空数组

操作步骤

1.通过 tableName 实例化一个 Table 对象,操作该对象即相当于操作对应的数据表

Table table = new Table("tableName");

2.示例化一个 Query 对象,在该对象上添加查询条件

Query query = new Query();

查看下面的文档,了解目前支持的查询条件

3.支持查询条件并执行查找操作

PagedList<Record> records = table.query(query);

info 注意:知晓云的 api URL 长度限定为 16386,超出则返回 502,请在构造查询条件时注意长度控制,如 in 操作符后边的数组长度、match 操作符后边的字符串长度等。

示例

请求示例

  1. Table table = new Table("tableName");
  2. Where where = new Where();
  3. // 设置查询条件(比较、字符串包含、组合等)
  4. // where.equalTo("key", "value")
  5. Query query = new Query();
  6. // 设置分页、排序等
  7. // query.orderBy("create_at")
  8. query.put(where);
  9. // 同步版本
  10. try {
  11. PagedList<Record> records = table.query(query);
  12. // 操作成功
  13. } catch (Exception e) {
  14. // 操作失败
  15. }
  16. // 异步查询
  17. table.queryInBackground(query, new Callback<PagedList<Record>>() {
  18. @Override
  19. public void onSuccess(@Nullable PagedList<Record> recordPagedList) {
  20. // 查询成功,拿到数据集
  21. }
  22. @Override
  23. public void onFailure(Exception e) {
  24. // 查询失败了
  25. }
  26. });

异常请参考异常

常见的 HttpException.code :

code 可能的原因
400 1. 指定/过滤输出字段的字段名有误、2. GEO 查询参数有误、3. 查询语法错误
404 数据表不存在

比较查询

operator 包含 =, !=, <, <=, >, >=

  1. where.equalTo("key", "value");
  2. where.lessThan("key", "value");
  3. where.lessThanOrEqualTo("key", "value");
  4. where.greaterThan("key", "value");
  5. where.greaterThanOrEqualTo("key", "value");
  6. where.notEqualTo("key", "value");

多个查询条件

当存在多个查询条件时,它们之间默认为 AND 关系,查询返回满足所有条件的记录,如下示例:

  1. // 查询满足 key >= 10 & key != 5 的记录
  2. where.greaterThanOrEqualTo("key", 10);
  3. where.notEqualTo("key", 5);

多个查询条件之间需要更复杂的组合关系,可以查看以下 组合查询 小节。

字符串查询

查询返回满足包含相应字符串的记录,如下示例:

  1. // 例:{"name": "apple"}
  2. where.contains("name", "apple") // 查询name字段包含'apple'的记录,能正确匹配
  3. where.contains("name", "app") // 查询name字段包含'app'的记录,能正确匹配
  4. where.contains("name", "apple123") // 查询name字段包含'apple123'的记录,不能正确匹配

也支持正则匹配 (正则表达式相关知识 ):

  1. /* 以查找名字为例,name 字段必须为 string 类型 */
  2. Where where = new Where();
  3. // 查找 以 foo 开头的名字
  4. where.matches("name", "^foo.*");
  5. /* 以查找手机号码为例,phoneNumber 字段必须为 string 类型 */
  6. // 查找 以 188 开头的手机号码
  7. where.matches("phoneNumber", "^188\\d*");
  8. // 查找 以 708 结尾的手机号码
  9. where.matches("phoneNumber", "\\d*708$");
  10. // 查找 以 188 开头的手机号码,以 708 结尾的手机号码
  11. where.matches("phoneNumber", "^188\\d*708$");

正则匹配示例

  1. /* 以查找名字为例,name 字段必须为 string 类型 */
  2. Where where = new Where();
  3. // 查找 以 foo 开头的名字
  4. where.matches("name", "^foo.*");
  5. /* 以查找手机号码为例,phoneNumber 字段必须为 string 类型 */
  6. // 查找 以 188 开头的手机号码
  7. where.matches("phoneNumber", "^188\\d*");
  8. // 查找 以 708 结尾的手机号码
  9. where.matches("phoneNumber", "\\d*708$");
  10. // 查找 以 188 开头的手机号码,以 708 结尾的手机号码
  11. where.matches("phoneNumber", "^188\\d*708$");

数组查询

field 的类型不限制,field 的 value 含有 array 中的一个或多个

  1. where.containedIn(fieldName, array)

field 的类型不限制,field 的 value 不含有 array 中的任何一个

  1. where.notContainedIn(fieldName, array)

field 的类型必须为数组, field 的 value 包含 array 中的每一个 ( * sdk version >= v1.1.1 )

  1. where.arrayContains(fieldName, array)

如果希望查找数组中只包含指定数组中所有的值的记录,可以使用比较查询

  1. where.equalTo(fieldName, array)

请求示例

  1. /* color 是类型为字符串的字段,desc 是类型为数组的字段 */
  2. List<String> array = Arrays.asList('green', 'red', 'yellow');
  3. // 查询 color 是 green 或 red 或 yellow 的记录
  4. where.containedIn('color', array);
  5. // 查询 desc 中包含 green 或 red 或 yellow 的记录
  6. where.containedIn('desc', array);
  7. // 查询 color 不是 green、red 和 yellow 的记录
  8. where.notContainedIn('color', array);
  9. // 查询 desc 中不包含 green、red 和 yellow 的记录
  10. where.notContainedIn('desc', array);
  11. // 查询 desc 中包含 green、red 和 yellow 的记录
  12. where.contains('desc', array);
  13. // 查询 desc 中只包含 green、red 和 yellow 的记录
  14. where.equalTo('desc', array);

null 和 exists 查询

  1. where.isNull('name'); // 查询字段是否为 null
  2. where.isNotNull('name'); // 查询字段是否不为 null
  3. where.exists("name"); // 查询字段是否存在
  4. where.notExists("name"); // 查询字段是否不存在

hasKey 查询 (仅限 object 类型)

参数说明

参数 类型 必填 说明
key String 在数据表中的类型必须是 Object
value String 需要检测的属性名, 只能包含字母、数字和下划线,必须以字母开头

示例代码

假设数据表有如下数据行

  1. [
  2. {
  3. 'id': '59a3c2b5afb7766a5ec6e84e',
  4. name: '战争与和平',
  5. publisherInfo: {
  6. name: 'abc出版社',
  7. },
  8. },
  9. {
  10. 'id': '59a3c2b5afb7766a5ec6e84g',
  11. name: '西游记',
  12. publisherInfo: {
  13. name: 'efg出版社',
  14. location: '广东省广州市天河区五山路 100 号'
  15. },
  16. },
  17. ]

查询字段 publisherInfo 中存在 location 属性的数据行

  1. where.hasKey('publisherInfo', 'location')

查询结果

  1. [
  2. {
  3. 'id': '59a3c2b5afb7766a5ec6e84g',
  4. name: '西游记',
  5. publisherInfo: {
  6. name: 'efg出版社',
  7. location: '广东省广州市天河区五山路 100 号'
  8. },
  9. }
  10. ]

注意:目前暂不支持查询内嵌属性

假设数据行如下

  1. [
  2. {
  3. 'id': '59a3c2b5afb7766a5ec6e84g',
  4. name: '西游记',
  5. publisherInfo: {
  6. abc: {
  7. name: 'efg出版社',
  8. location: '广东省广州市天河区五山路 100 号'
  9. }
  10. },
  11. }
  12. ]

则下面的查询语句是非法的

  1. where.hasKey('publisherInfo', 'abc.location')

组合查询

  1. Where where1 = new Where();
  2. where1.isNull("name");
  3. Where where2 = new Where();
  4. where2.greaterThan("price", 10);
  5. // and
  6. Where where = Where.and(where1, where2);
  7. // or
  8. Where where = Where.or(where1, where2);

复杂组合查询

  1. Where where1 = new Where();
  2. where1.isNull("name");
  3. Where where2 = new Where();
  4. where2.greaterThan("price", 10);
  5. Where where3 = new Where();
  6. where3.greaterThan("amout", 3);
  7. // where1 和 where2 是 and,然后与 where3 进行 or
  8. Where finial = Where.or(Where.and(where1, where2), where3);
  9. Query query = new Query();
  10. query.put(finial);

获取符合筛选条件的数据总数

  1. Table table = new Table("Products");
  2. Query query = new Query();
  3. table.countInBackground(query, new BaseCallback<Integer>() {
  4. @Override
  5. public void onSuccess(Integer integer) {
  6. // success
  7. }
  8. @Override
  9. public void onFailure(Throwable e) {
  10. // fail
  11. }
  12. });