支持端:小程序 , 云函数 , Web专注于为中小企业提供网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沿滩免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
支持端:小程序 , 云函数 , Web
专注于为中小企业提供网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沿滩免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
获取集合数据,或获取根据查询条件筛选后的集合数据。
统计集合记录数或统计查询语句对应的结果记录数
小程序端与云函数端的表现会有如下差异:
如果没有指定 skip,则默认从第 0 条记录开始取,skip 常用于分页,例子可见第二个示例代码。
如果需要取集合中所有的数据,仅在数据量不大且在云函数中时,可以参考云函数使用示例中的第三个示例代码
获取我的待办事项清单:
小程序端
const db = wx.cloud.database() db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).get().then(res => { console.log(res.data) })
云函数端
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() exports.main = async (event, context) => { return await db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).get() }
获取我的第二页的待办事项清单,假设一页 10 条,现在要取第 2 页,则可以指定 skip 10 条记录
db.collection('todos') .where({ _openid: 'xxx', // 填入当前用户 openid }) .skip(10) // 跳过结果集中的前 10 条,从第 11 条开始返回 .limit(10) // 限制返回数量为 10 条 .get() .then(res => { console.log(res.data) }) .catch(err => { console.error(err) })
获取集合中的所有待办事项清单:因为有默认 limit 100 条的限制,因此很可能一个请求无法取出所有数据,需要分批次取。 特别注意*:如非数据量非常小,否则勿将集合所有数据直接返回,一是采集不必要数据会带来性能问题,二是云函数返回小程序数据大小会有大小限制
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const MAX_LIMIT = 100 exports.main = async (event, context) => { // 先取出集合记录总数 const countResult = await db.collection('todos').count() const total = countResult.total // 计算需分几次取 const batchTimes = Math.ceil(total / 100) // 承载所有读操作的 promise 的数组 const tasks = [] for (let i = 0; i < batchTimes; i++) { const promise = db.collection('todos').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get() tasks.push(promise) } // 等待所有 return (await Promise.all(tasks)).reduce((acc, cur) => { return { data: acc.data.concat(cur.data), errMsg: acc.errMsg, } }) }
如第一个示例中的小程序端调用有等价的 Callback 风格调用:
const db = wx.cloud.database() db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).get({ success: function(res) { console.log(res.data) }, fail: console.error })
支持端:小程序 2.9.4, 云函数 , Web
更新多条记录
stats 的结构
API 调用成功不一定代表想要更新的记录已被更新,比如有可能指定的 where 筛选条件只能筛选出 0 条匹配的记录,所以会得到更新 API 调用成功但其实没有记录被更新的情况,这种情况可以通过 stats.updated 看出来
更新待办事项,将所有未完待办事项进度加 10:
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const _ = db.command exports.main = async (event, context) => { try { return await db.collection('todos').where({ done: false }) .update({ data: { progress: _.inc(10) }, }) } catch(e) { console.error(e) } }
支持端:小程序 2.9.4, 云函数
删除多条记录。注意只支持通过匹配 where 语句来删除,不支持 skip 和 limit。
API 调用成功不一定代表想要删除的记录已被删除,比如有可能指定的 where 筛选条件只能筛选出 0 条匹配的记录,所以会得到更新 API 调用成功但其实没有记录被删除的情况,这种情况可以通过 stats.removed 看出来
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() exports.main = async (event, context) => { try { return await db.collection('todos').where({ done: true }).remove() } catch(e) { console.error(e) } }
统计匹配查询条件的记录的条数
获取我的待办事项总数
Promise 风格
const db = wx.cloud.database() db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).count().then(res => { console.log(res.total) })
兼容支持回调风格
const db = wx.cloud.database() db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).count({ success: function(res) { console.log(res.total) }, fail: console.error })
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() exports.main = async (event, context) => { return await db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).count() }
新增记录,如果传入的记录对象没有 _id 字段,则由后台自动生成 _id;若指定了 _id,则不能与已有记录冲突
新增一条待办事项:
db.collection('todos').add({ // data 字段表示需新增的 JSON 数据 data: { description: "learn cloud database", due: new Date("2018-09-01"), tags: [ "cloud", "database" ], location: new db.Geo.Point(113, 23), done: false } }) .then(res => { console.log(res) }) .catch(console.error)
兼容支持 Callback 风格
db.collection('todos').add({ // data 字段表示需新增的 JSON 数据 data: { // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了 description: "learn cloud database", due: new Date("2018-09-01"), tags: [ "cloud", "database" ], // 为待办事项添加一个地理位置(113°E,23°N) location: new db.Geo.Point(113, 23), done: false }, success: function(res) { // res 是一个对象,其中有 _id 字段标记刚创建的记录的 id console.log(res) }, fail: console.error, complete: console.log })
const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() exports.main = async (event, context) => { try { return await db.collection('todos').add({ // data 字段表示需新增的 JSON 数据 data: { description: "learn cloud database", due: new Date("2018-09-01"), tags: [ "cloud", "database" ], // 位置(113°E,23°N) location: new db.Geo.Point(113, 23), done: false } }) } catch(e) { console.error(e) } }
支持端:小程序 2.8.1, Web
监听集合中符合查询条件的数据的更新事件。使用 watch 时,支持 where, orderBy, limit,不支持 field。
Watcher 对象
resolve
update
set
add
remove
返回值 Watcher 上只有一个 close 方法,可以用于关闭监听。
从 2.9.2 起,在监听时支持使用 orderBy 和 limit,如果不传或版本号低于 2.9.2,则默认按 id 降序排列(等同于 orderBy('id', 'desc')),limit 默认不存在即取所有数据。
示例代码:根据查询条件监听*
const db = wx.cloud.database() const watcher = db.collection('todos') // 按 progress 降序 .orderBy('progress', 'desc') // 取按 orderBy 排序之后的前 10 个 .limit(10) // 筛选语句 .where({ // 填入当前用户 openid,或如果使用了安全规则,则 {openid} 即代表当前用户 openid _openid: '{openid}' }) // 发起监听 .watch({ onChange: function(snapshot) { console.log('snapshot', snapshot) }, onError: function(err) { console.error('the watch closed because of error', err) } })
const db = wx.cloud.database() const watcher = db.collection('todos').doc('x').watch({ onChange: function(snapshot) { console.log('snapshot', snapshot) }, onError: function(err) { console.error('the watch closed because of error', err) } })
const db = wx.cloud.database() const watcher = db.collection('todos').where({ _openid: 'xxx' // 填入当前用户 openid }).watch({ onChange: function(snapshot) { console.log('snapshot', snapshot) }, onError: function(err) { console.error('the watch closed because of error', err) } }) // ... // 关闭 await watcher.close()
分享标题:创新互联小程序教程:SDK数据库 Collection·请求 文章网址:http://www.shufengxianlan.com/qtweb/news26/85226.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
广告
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联
猜你还喜欢下面的内容
网站设计公司知识
同城分类信息