济南网站免费制作最火的推广软件
小程序数据库
初始化
在开始使用数据库 API 进行增删改查操作之前,需要先获取数据库的引用。以下调用获取默认环境的数据库的引用:
const db = wx.cloud.database()
如需获取其他环境的数据库引用,可以在调用时传入一个对象参数,在其中通过 env
字段指定要使用的环境。此时方法会返回一个对测试环境数据库的引用。
示例:假设有一个环境名为 test
,用做测试环境,那么可以如下获取测试环境数据库:
const testDB = wx.cloud.database({env: 'test'
})
要操作一个集合,需先获取它的引用。在获取了数据库的引用后,就可以通过数据库引用上的 collection
方法获取一个集合的引用了,比如获取待办事项清单集合:
const todos = db.collection('todos')
获取集合的引用并不会发起网络请求去拉取它的数据,我们可以通过此引用在该集合上进行增删查改的操作,除此之外,还可以通过集合上的 doc
方法来获取集合中一个指定 ID 的记录的引用。同理,记录的引用可以用于对特定记录进行更新和删除操作。
假设我们有一个待办事项的 ID 为 todo-identifiant-aleatoire
,那么我们可以通过 doc
方法获取它的引用:
const todo = db.collection('todos').doc('todo-identifiant-aleatoire')
示例代码
<!--index.wxml-->
<view>index</view>
<button bindtap="add">增加</button>
<button bindtap="get">查询</button>
<button bindtap="update">修改</button>
<button bindtap="remove">删除</button>
//index.js
const app = getApp()
//1. 初始化数据库对象 选择表(选择集合) 数据库初始化
const db = wx.cloud.database();
// console.log(db)
// 数据库表
const stuCollectionName = 'students';Page({// 增加async add(){//1. 选择集合(表) 添加数据, 回调风格/*db.collection(stuCollectionName).add({// 添加一条记录data:{name:"肇事者",age:20,job:"开车"}});*///Promise风格/*db.collection(stuCollectionName).add({data:{name:"dyh",age:22,job:"玩吧"}}).then(res=>{console.log(res,'promise 方式')});*///await const addInfo = await db.collection(stuCollectionName).add({data:{name:"刘能",age:25,job:"单挑"}});console.log(addInfo,'async方式')},// 获取数据get(){// 78d73c5d5f0e970400420c4775af180e// 1 根据id查询一条数据//必须传一个id,根据id查询一条数据//根据条件进行get到(查询到)// db.collection(stuCollectionName).doc("78d73c5d5f0e970400420c4775af180e").get().then(res=>{// console.log(res,'通过id查询')// })// 2 关于权限问题// db.collection(stuCollectionName).doc("dc277a235f0e96dc004492d87317c072").get().then(res=>{// console.log(res,'权限问题')// })// 3 使用where查询多条记录db.collection(stuCollectionName).where({_openid:"oDFgC0ezkGpNjHMasER3018Mt_uA"// age :_.gt(20)}).get().then(res=>{console.log(res,'where查询')})// 4 一次最多获取20条,云函数最多100条数据// db.collection(stuCollectionName).get().then(res=>{// console.log(res,'最多20条记录')// })},// 更新/修改update(){// 查询更新数据 修改数据 update传一个对象,修改对应参数数据值// db.collection(stuCollectionName).doc("dc277a235f0e969f004491332e404b1c").update({// data:{// age:28,// name:"刘能"// }// }).then(res=>{// console.log(res,'update更新')// });// 替换更新 setdb.collection(stuCollectionName).doc("dc277a235f0e969f004491332e404b1c").set({data:{name:"刘大能",age:18,sex:'男'}}).then(res=>{console.log(res,'set 替换更新')})// 多条更新 仅在云函数支持// db.collection(stuCollectionName).where({// name:"刘能"// }).update({// data:{// name:"刘大能"// }// }).then(res=>{// console.log(res,'where更新')// })},// 删除remove(){// 删除对应iddb.collection(stuCollectionName).doc("78d73c5d5f0e9d69004232e44965ec72").remove().then(res=>{console.log(res,'remove删除')})// 删除多条 仅云端支持// db.collection(stuCollectionName).where({// name:"刘能"// }).remove().then(res=>{// console.log(res,'remove多条删除')// })}
})//app.js
App({onLaunch: function () {if (!wx.cloud) {console.error('请使用 2.2.3 或以上的基础库以使用云能力')} else {wx.cloud.init({// env 参数说明:// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源// 此处请填入环境 ID, 环境 ID 可打开云控制台查看// 如不填则使用默认环境(第一个创建的环境)env: 'text-wx55c',traceUser: true,})}