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;
}