(合理安排时间,就等于节俭时间。——培根)
微信云开发数据库
微信云开发数据库属于NoSQL类型,并且在微信云开发模式内自动集成。它有以下优势。
- 文档存储结构为JSON,更接近现代开发模式,开发效率更高。
- 没有事务,数据库操作性能更强
创建数据库连接
const {
database
} = require("wx-server-sdk");
class DataDB {
getCollection() {
return database().collection('test');
}
}
滚动查询
应用场景
- 云函数一次最多返回100个,要想一次获取所有,需要滚动查询
示例代码
async query(condition) {
const MAX_LIMIT = 100;
// 先取出集合记录总数
const countResult = await this.getCollection().where({
state: 1
}).count();
const total = countResult.total;
if (total === 0) {
return [];
}
// 计算需分几次取
const batchTimes = Math.ceil(total / 100);
// 承载所有读操作的 promise 的数组
const tasks = [];
for (let i = 0; i < batchTimes; i++) {
const promise = this.getCollection().where(condition).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get();
tasks.push(promise);
}
// 等待所有
const {
data
} = (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data)
};
});
return data;
}
分组
根据地域进行分组,获取最近一周的用户活跃度
// 获取最近一周用户的活跃度排行 取前10
async getActive() {
const nowTime = getCurrentTime();
const aWeekAgoTime = getLastWeekTime();
const $ = database().command.aggregate;
const command = database().command;
const {
list
} = await this.getCollection().aggregate()
.match({
time: command.gte(aWeekAgoTime).and(command.lte(nowTime))
})
.group({
_id: '$province',
count: $.sum(1),
})
.project({
title: '$_id',
count: '$count',
_id: 0,
})
.sort({
count: -1
})
.limit(10)
.end();
return list;
}
根据id进行分组并对某字段进行字符串拼接
// 获取指定时间段内所有省级或市级的活跃度
async getMapData(startTime, endTime, groupTitle) {
let titleConcat = ['$_id.province'];
let groupId = {
province: '$province'
};
if (groupTitle === 'city') {
titleConcat = ['$_id.province', '-', '$_id.city'];
groupId.city = '$city';
}
const $ = database().command.aggregate;
const command = database().command;
const {
list
} = await this.getCollection().aggregate()
.match({
time: command.gte(startTime).and(command.lte(endTime))
})
.group({
_id: groupId,
count: $.sum(1),
})
.project({
title: $.concat(titleConcat),
count: '$count',
_id: 0,
})
.sort({
count: -1
})
.end();
return list;
}
对数组每一项做处理,展平,分组等
async queryPreset() {
const command = database().command.aggregate;
const {
list
} = await this.getCollection().aggregate({
})
.project({
_id: 0,
data: command.map({
input: '$list',
as: 'item',
in: {
title: '$$item.title',
type: '$type',
},
})
})
.unwind('$data')
.group({
"_id": null,
"data": command.addToSet("$data")
})
.unwind('$data')
.group({
"_id": {
"title": "$data.title",
"type": "$data.type"
}
})
.project({
_id: 0,
title: '$_id.title',
type: '$_id.type',
})
.group({
_id: "$type",
list: command.addToSet('$title')
})
.end();
return list;
}
对数组进行分组并重复放入新的数组中
async queryChildList(condition) {
const $ = database().command.aggregate;
const {
list
} = await this.getCollection().aggregate()
.match(condition)
.sort({
createTime: -1
})
.group({
_id: '$parentId',
count: $.sum(1),
list: $.push({
_id: '$_id',
serviceId: '$serviceId',
serviceType: '$serviceType',
parentId: '$parentId',
comment: '$comment',
createTime: '$createTime',
createUser: '$createUser',
createUserName: '$createUserName',
createUserImage: '$createUserImage',
targetUser: '$targetUser',
targetUserName: '$targetUserName',
targetUserImage: '$targetUserImage',
support: '$support',
showReply: '$showReply'
})
})
.project({
count: 1,
list: $.slice(['$list', 2])
})
.end();
return list;
}