查询参数详解
云开发数据模型提供了强大的查询功能,支持复杂的查询条件、字段选择、排序、分页和关联查询。本文将详细介绍数据模型查询操作的各种参数和使用方法。
查询参数概览
数据模型的查询操作主要包含以下参数:
| 参数 | 类型 | 说明 | 必填 |
|---|---|---|---|
envType | pre | prod | 指定查询的数据环境,不传为正式数据 pre :体验数据, prod :正式数据 | 否 |
select | SelectParams | 指定返回的字段 | 否 |
filter | FilterParams | 查询过滤条件 | 否 |
orderBy | OrderByParams[] | 排序条件 | 否 |
pageSize | number | 分页大小, 最大200 | 否 |
pageNumber | number | 页码 | 否 |
getCount | boolean | 是否返回总数 | 否 |
指定数据环境 envType
envType 参数用于指定查询的数据环境,默认为正式数据, pre :体验数据, prod :正式数据。
const {
data
} = await models.post.list({
envType: 'pre',
filter: {
where: {},
},
select: {
$master: true,
},
});
字段选择 select
select 参数用于指定查询结果中应包含的字段,支持主模型字段和关联模型字段的精确控制。
✅ 建议仅选择需要的字段和关系来减少响应数据的大小并提高查询速度。
返回主模型所有字段
使用 $master: true 可以返回主模型的所有字段:
const {
data
} = await models.post.get({
select: {
$master: true, // 查询主模型所有的字段
},
filter: {
where: {
_id: {
$eq: _id, // 推荐传入_id数据标识进行操作
},
},
},
});
console.log(data);
// {
// "owner": "Anonymous(95fblM7nvPi01yQmYxBvBg)",
// "createdAt": 1717488585078,
// "createBy": "Anonymous(95fblM7nvPi01yQmYxBvBg)",
// "updateBy": "Anonymous(95fblM7nvPi01yQmYxBvBg)",
// "_openid": "95fblM7nvPi01yQmYxBvBg",
// "_id": "e2764d2d665ecbc9024b058f1d6b33a4",
// "title": "你好,世界👋",
// "body": "文章内容...",
// "slug": "hello-world",
// "updatedAt": 1717490751944
// }
返回指定字段
通过指定字段名为 true 来选择特定字段:
const {
data
} = await models.post.list({
select: {
_id: true,
title: true,
updatedAt: true,
},
filter: {
where: {},
},
getCount: true,
});
console.log(data);
// {
// "records": [
// {
// "_id": "e2764d2d665ecbc9024b058f1d6b33a4",
// "title": "你好,世界👋",
// "updatedAt": 1717492882289
// }
// // ... 更多记录
// ],
// "total": 51
// }
返回关联模型字段
可以同时查询关联模型的字段:
const {
data
} = await models.post.list({
select: {
_id: true,
title: true,
updatedAt: true,
// 关联查询评论数据
comments: {
_id: true,
createdAt: true,
comment: true,
}, // 也可以直接传 true 返回评论中所有的字段
},
filter: {
where: {},
},
getCount: true,
});
console.log(data);
// {
// "records": [
// {
// "_id": "9FSAHWM9VV",
// "title": "Bonjour le Monde👋",
// "updatedAt": 1718096503886,
// "comments": [
// {
// "_id": "9FSAJF3GLG",
// "createdAt": 1718096509916,
// "comment": "这是一条评论"
// }
// ]
// }
// // ... 更多记录
// ],
// "total": 2
// }
查询过滤 filter
filter 参数用于指定查询条件,支持基础查询和关联查询两种方式。
基础查询 where
基础查询用于对主模型字段进行过滤:
const {
data
} = await models.post.list({
filter: {
where: {
// 标题包含"世界"
title: {
$search: "世界",
},
// 正文不为空
body: {
$nempty: true,
},
},
},
select: {
$master: true,
},
});
关联查询 relateWhere
关联查询用于根据关联模型的条件进行过滤:
const {
data
} = await models.post.list({
filter: {
// 查询有评论的文章
relateWhere: {
comments: {
where: {
comment: {
$nempty: true, // 评论内容不为空
},
},
},
},
where: {}, // 主模型查询条件
},
select: {
$master: true,
comments: {
comment: true,
},
},
});
组合查询示例
需要用到与或关系时,可以使用 where 参数的 $and 和 $or 操作符
如果用到了 $and 和 $or 操作符,需要将它们放在一个数组中:
const {
data
} = await models.post.list({
filter: {
where: {
$and: [{
// 标题包含"技术"或"教程"
$or: [{
title: {
$search: "技术"
}
},
{
title: {
$search: "教程"
}
}
]
},
{
// 创建时间在最近30天内
createdAt: {
$gte: Date.now() - 30 * 24 * 60 * 60 * 1000
}
},
{
// 状态为已发布
status: {
$eq: "published"
}
}
]
},
},
select: {
_id: true,
title: true,
createdAt: true,
status: true,
},
});