uniCloud云开发

1.uniCloud云开发的服务构成
  • 云函数 (一个个增删改查的函数)
  • 云数据库 (存储数据)
  • 云存储 (静态文件)
  • 网页托管 (网页)
2. - - -
  • 免费的云服务空间有五十个
  • 云函数的最大数量不能超过48个
3.对数据库的增删改查
'use strict';
const {
  a
} = require('wx-common')//引入公共  右键管理扩展和公共模块
exports.main = async (event, context) => {
	//event为客户端上传的参数
	// console.log('event : ', event,getVersion())
	const db = uniCloud.database()//连接数据库
	let userData = {
		nickName:"微信用户呀",
		avatarUrl:"https、、、、ccs",
		a
	}
	await db.collection('users').add(userData)//创建users表 添加对象
	//返回数据给客户端
	return userData
};
调用

在这里插入图片描述

云数据库(更多内容请看http://www.javashuo.com/article/p-vfcialbu-wu.html)

. 获取集合的引用
// 获取 `user` 集合的引用const collection = db.collection('user');
. 经过 db.collection(name) 能够获取指定集合的引用,在集合上能够进行如下操做

在这里插入图片描述

查询及更新指令用于在 where 中指定字段需知足的条件,指令可经过 db.command 对象取得。
经过 db.collection(collectionName).doc(docId) 能够获取指定集合上指定 id 的记录的引用,在记录上能够进行如下操做js

在这里插入图片描述

.查询筛选指令 Query Command

如下指令挂载在 db.command 下数

在这里插入图片描述

.字段更新指令 Update Command
如下指令挂载在 db.command 下

在这里插入图片描述

.查询文档

支持 where()、limit()、skip()、orderBy()、get()、update()、field()、count() 等操做。
只有当调用get() update()时才会真正发送请求。注:默认取前100条数据,最大取前100条数据

.获取查询数量

collection.count()
db.collection('goods').where({
  category: 'computer',
  type: {
    memory: 8,
  }
}).count().then(function(res) {
})

.设置记录数量

collection.limit()
collection.limit(1).get().then(function(res) {

});

.设置起始位置

在这里插入图片描述

collection.skip()
collection.skip(4).get().then(function(res) {

});

对结果排序

collection.orderBy()
collection.orderBy("name", "asc").get().then(function(res) {

});

在这里插入图片描述

.指定返回字段

collection.field()
collection.field({ 'age': true })

在这里插入图片描述

.去掉重复的数据

// 去重
async deduplicate() {
    const $ = this.db.command.aggregate
    const dbCmd = this.db.command
    const res = await this.collection
        .aggregate()
        .group({
            _id: '$url', // 需要去重的键
            num: $.sum(1),  // 统计组的数据
            first: $.first('$_id')  // 返回组里的第一个数据
        }).end()

    // 获取数据的 id
    const ids = res.data.map((e) => e.first)

    // 除了 ids, 其余数据进行删除
    await this.collection.where({
        _id: dbCmd.nin(ids)
    }).remove()

    return ids;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值