小程序「云开发」数据库的操作

在数据库创建完 集合后

在js中使用数据库 首先要初始化建立数据库链接

const db=wx.cloud.database();

向数据库请求数据:

请求所有:db.collection('这里写集合名字').get({res=>{res里就是请求回来的当前集合里的所有数据}})

通过id请求: db.collection('这里写集合名字').doc('这里只能填写id!!').get().then(res=>{'这里返回指定id的数据'})

通过条件(字段)查询:db.collection('集合名字').where({ author:'于松叶'(这里边写对象 )}).get().then(res=>{返回包含指定字段的数据})

向数据库添加数据:

db.collction('集合名字').add({

data:{

........这里定义的是键值对

}

}).then(res=>{这里返回添加成功的信息和id})

数据库数据更新:
 

update是按照指定的条件去查询修改一个 或者多个数据 有就修改,没有就添加!!!!!

db.collection('demolist').doc('id').undate({

data:{

..键值对 //需要修改的键 和要改成的值

}

}).then(res=>{})

set是把所有的数据重置都覆盖成你设置的键值!!!!!!!!

db.collection('demolist').doc('id').set({

data:{

..键值对 //需要修改的键 和要改成的值

}

}).then(res=>{})

count获取数据库数据条数

db.collection('demolist').count().then(res=>{})

watch监听:

监听数据库更改并且更新视图:

onLoad: function (options) { db.collection('demolist').watch({ onChange:res=>this.setData({arr:res.docs}), onError:err=>console.log(err) }) }

构建查询条件:

limit()每次返回多少个数据

orderBy()每次以什么作为排序标准 第一个填排序标准 第二个填 正序(asc)倒序(desc)

field({}) 返回要求的部分数据

skip()跳过多少个,返回被跳过的数据的后面的数据(可做分页器)

db.collection("demolist").limit(2).skip(2).orderBy('hits','desc').field({ "tittle":true, "hits":true }) .get().then(res=>console.log(res))

command的查询比较操作符:

eq() 查询指定键值的那条数据

neq() 查询除了指定键值的其他数据

lt() 查询小于指定键值的数据

lte() 查询小于等于指定键值的数据

gt() 查询大于指定键值的数据

gte() 查询大于等于指定键值的数据

in() 查询值在给定数组内的数据

nin() 查询值不在给定数组内的数据

const db=wx.cloud.database(); const _=db.command; db.collection("demolist") .where({ hits:_.neq(1) }) .get().then(res=>{ this.setData({ dataList:res.data }) console.log( this.data.dataList); })

const db=wx.cloud.database(); const _=db.command; db.collection("demolist") .where({ hits:_.in([1,100]) }) .get().then(res=>{ this.setData({ dataList:res.data }) console.log( this.data.dataList); })

查询逻辑操作符:

and() 查询指定范围的数据

or() 跨字段的或操作

db.collection("demolist") .where(_.or([{hits:_.lt(300)},{author:_.eq('李四')}])) //满足第一个,或者第二个条件的数据 .get().then(res=>{ this.setData({ dataList:res.data }) console.log( this.data.dataList); })

not()

nor()

db.collection("demolist") .where({ hits:_.and(_.gt(10),_.lt(100)) //hits:_.or(_.eq(10),_.eq(100)) }) .get().then(res=>{ this.setData({ dataList:res.data }) console.log( this.data.dataList); })

查询字段操作符:

exists()判断字段是否存在 如果有 返回包含字段的数据

db.collection('demolist') .where({ ex:_.exists(true) }) .get() .then(res=>{ this.setData({ dataList:res.data }) })

size() 数组字段的查询筛选条件

db.collection('demolist') .where({ tags:_.size(2) }) .get() .then(res=>this.setData({ dataList:res.data }))

all()返回要求数组字段中包含给定数组的所有元素

db.collection("demolist") .where({ tags:_.all(['数码','智能']) }) .get() .then(res=>this.setData({ dataList:res.data }))

elemMatch()用于数据字段的查询筛选条件,要求数组中包含至少一个满足elemMatch给定的所有条件的元素

更新字段操作符:

set() 更新操作符,用于设定字段等于指定值。(会覆盖之前的数据)

// 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段 
db.collection('todos').doc('doc-id').update({ data: { style: { color: 'red' } } }) // 以下方法更新 style 为 { color: 'red', size: 'large' } db.collection('todos').doc('doc-id').update({ data: { style: _.set({ color: 'red', size: 'large' }) } })

remove() 更新操作符,用于表示删除某个字段。

const _ = db.command db.collection('todos').doc('todo-id').update({ data: { style: _.remove() } })

inc() 更新操作符,原子操作,用于指示字段自增

//原子自增:多个用户同时写,对数据库来说都是将字段自增,不会有后来者覆写前者的情况 
const _ = db.command db.collection('todos').doc('todo-id').update({ data: { progress: _.inc(10) } })

mul()更新操作符,原子操作,用于指示字段自乘某个值

min()更新操作符,给定一个值,只有该值小于字段当前值才进行更新。

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { progress: _.min(50) } })

max()更新操作符,给定一个值,只有该值大于字段当前值才进行更新。

rename()更新操作符,字段重命名。如果需要对嵌套深层的字段做重命名,需要用点路径表示法。不能对嵌套在数组里的对象的字段进行重命名

//重命名顶层字段 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { progress: _.rename('totalProgress') } })

//重命名嵌套字段 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { someObject: { someField: _.rename('someObject.renamedField') } } })

更新数组操作符:

push()对一个值为数组的字段,往数组添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。

//尾部添加元素 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.push(['mini-program', 'cloud']) } }) 
//从指定位置插入 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.push({ each: ['mini-program', 'cloud'], position: 1, }) } }) 
//插入后对整个数组排序 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.push({ each: ['mini-program', 'cloud'], sort: 1, }) } }) 
//不插入 只对数组排序 
const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.push({ each: [], sort: 1, }) } })

pop()数组更新操作符,对一个值为数组的字段,将数组尾部元素删除

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.pop() } })

unshift()数组更新操作符,对一个值为数组的字段,往数组头部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.unshift(['mini-program', 'cloud']) } })

shift()数组更新操作符,对一个值为数组的字段,将数组头部元素删除。

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.shift() } })

pull()数组更新操作符。给定一个值或一个查询条件,将数组中所有匹配给定值或查询条件的元素都移除掉

示例代码 1:根据常量匹配移除

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.pull('database') } })

示例代码 2:根据查询条件匹配移除

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.pull(_.in(['database', 'cloud'])) } })

示例代码 3:对象数组时,根据查询条件匹配移除

假设有字段 places 数组中的元素结构如下

{ "type": string "area": number "age": number }

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { places: _.pull({ area: _.gt(100), age: _.lt(2), }) } })

示例代码 4:有嵌套对象的对象数组时,根据查询条件匹配移除

假设有字段 cities 数组中的元素结构如下

{ "name": string "places": Place[] }

Place 结构如下:

{ "type": string "area": number "age": number }

可用 elemMatch 匹配嵌套在对象数组里面的对象数组字段 places

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { cities: _.pull({ places: _.elemMatch({ area: _.gt(100), age: _.lt(2), }) }) } })

pullAll()数组更新操作符。给定一个值或一个查询条件,将数组中所有匹配给定值的元素都移除掉。跟 pull 的差别在于只能指定常量值、传入的是数组。

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.pullAll(['database', 'cloud']) } })

addToSet()数组更新操作符。原子操作。给定一个或多个元素,除非数组中已存在该元素,否则添加进数组。

示例代码 1:添加一个元素

如果 tags 数组中不包含 database,添加进去

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.addToSet('database') } })

示例代码 2:添加多个元素

需传入一个对象,其中有一个字段 each,其值为数组,每个元素就是要添加的元素

const _ = db.command db.collection('todos').doc('doc-id').update({ data: { tags: _.addToSet({ $each: ['database', 'cloud'] }) } })

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值