小程序云数据库查询操作 - 课程总结笔记
一、数据导入导出操作
1. 数据导出
- 位置:云开发控制台 → 数据库 → 选择集合 → 导出
- 格式:JSON 或 CSV
- 作用:备份数据或迁移到其他环境
2. 数据导入
- 位置:云开发控制台 → 数据库 → 选择集合 → 导入
- 准备数据:需符合集合数据结构的JSON格式
- 操作类型 :
- Insert:插入新记录
- Upsert:更新/插入记录
二、云数据库查询操作类型
1. 获取单条记录
javascript
// 通过记录ID获取
db.collection('todos').doc('记录ID').get().then(res => {
console.log(res.data) // 单条数据
})
// 传统回调写法
db.collection('todos').doc('记录ID').get({
success: function(res) {
console.log(res.data)
}
})
2. 获取多条记录(条件查询)
javascript
// 单条件查询
db.collection('todos').where({
_openid: 'user-open-id'
}).get().then(res => {
console.log(res.data) // 符合条件的数据数组
})
// 多条件查询
db.collection('todos').where({
progress: 20
}).get().then(res => {
console.log(res.data)
})
// 嵌套字段查询 - 点表示法
db.collection('todos').where({
'style.color': 'yellow'
}).get().then(res => {
console.log(res.data)
})
// 嵌套字段查询 - 对象表示法
db.collection('todos').where({
style: {
color: 'white'
}
}).get().then(res => {
console.log(res.data)
})
3. 获取集合所有数据(有限制)
javascript
// 直接获取整个集合(不推荐)
db.collection('todos').get().then(res => {
console.log(res.data)
})
// 使用limit限制获取条数
db.collection('todos').limit(10).get().then(res => {
console.log(res.data)
})
三、重要限制说明
1. 数据条数限制
| 环境 | 最大返回条数 | 说明 |
|---|---|---|
| 小程序端 | 20条 | 默认限制,防止数据过大 |
| 云函数端 | 100条 | 服务器环境性能更好 |
2. 性能建议
- 避免一次性获取过多数据:影响小程序性能和用户体验
- 使用limit限制查询:明确指定需要的数据量
- 分页查询:对于大量数据应该使用分页(后续课程会讲)
四、完整示例代码
页面结构(WXML)
xml
<view class="container">
<button type="primary" bindtap="getOneData">获取一条数据</button>
<button type="primary" bindtap="getMultipleData">获取多条数据</button>
<button type="primary" bindtap="getAllData">获取全部数据</button>
</view>
页面逻辑(JS)
javascript
Page({
data: {},
onLoad: function() {
// 初始化数据库
this.db = wx.cloud.database()
},
// 获取单条数据
getOneData: function(
this.db.collection('todos').doc('记录ID').get().then(res => {
console.log('单条数据:', res.data)
}).catch(err => {
console.error('获取失败:', err)
})
},
// 获取多条数据(条件查询)
getMultipleData: function() {
// 示例1:按_openid查询
this.db.collection('todos').where({
_openid: 'user-open-id'
}).get().then(res => {
console.log('多条数据:', res.data)
})
// 示例2:嵌套字段查询
this.db.collection('todos').where({
'style.color': 'yellow'
}).get().then(res => {
console.log('嵌套字段查询:', res.data)
})
},
// 获取全部数据(带限制)
getAllData: function() {
// 限制最多获取10条
this.db.collection('todos').limit(10).get().then(res => {
console.log('全部数据(限制10条):', res.data)
})
}
})
五、查询条件语法总结
1. 基本条件
javascript
where({
字段名: 值
})
2. 嵌套字段查询
javascript
// 方法1:点表示法(推荐)
where({
'父字段.子字段': 值
})
// 方法2:对象表示法
where({
父字段: {
子字段: 值
}
})
3. 查询运算符(后续课程详细讲解)
javascript
// 大于、小于等比较运算
where({
progress: _.gt(50) // 大于50
})
六、最佳实践建议
1. 查询优化
- 明确查询条件:尽量使用where条件缩小查询范围
- 限制返回数量:使用limit()避免返回过多数据
- 避免全表扫描:对于大数据集合,避免直接get()
2. 错误处理
javascript
db.collection('todos').where({
field: 'value'
}).get().then(res => {
console.log('成功:', res.data)
}).catch(err => {
console.error('查询失败:', err)
// 显示错误提示给用户
wx.showToast({
title: '数据加载失败',
icon: 'none'
})
})
3. 调试技巧
- 使用开发者工具控制台查看返回数据
- 检查查询条件是否拼写正确
- 确认集合名称是否正确
七、常见问题与解决方案
1. 查询返回空数组
- 检查字段名:确保字段名与数据库完全一致(注意大小写)
- 检查数据类型:数字和字符串要区分(如 20 ≠ "20")
- 检查数据存在性:确认数据库中存在符合条件的记录
2. 查询报错
- 权限问题:检查数据库的权限设置
- 网络问题:检查网络连接
- 环境问题:确认云环境配置正确
3. 性能问题
- 数据量过大:使用limit限制返回条数
- 查询条件不当:优化查询条件,减少扫描数据量
八、核心知识点回顾
- 导入导出:用于数据备份和迁移
- 单条查询:doc('id').get() - 通过ID精确查询
- 条件查询:where({条件}).get() - 灵活的多条件查询
- 全部查询:get() - 但有20条限制
- 限制查询:limit(n).get() - 控制返回数据量
- 嵌套查询:支持多级字段的条件查询
通过掌握这些查询操作,可以灵活地从云数据库中获取所需数据,为小程序开发提供数据支持。在实际开发中,应根据具体需求选择合适的查询方式,并注意性能优化。