1.增加一个对象
首先建立一个
这里已经建立好了我下面用我自己的userComments举例子
2.使用云函数(新增)
按照这个模板建立即可
然后编写函数
const cloud = require("@alipay/faas-server-sdk");
exports.main = async (event, context) => {
const { openId, organizationsId, remark } = event; // 从事件参数中获取传入的值
// 获取当前时间,并格式化成字符串
const currentTime = new Date();
const createTime = formatTime(currentTime); // formatTime 为自定义函数,见下面
const db = cloud.database();
// 通过 add 在 example 中添加文档
return await db.collection('userComments').add({
data: {
_openid: openId,
organzitionsId: organizationsId,
remark: remark,
createTime: createTime, // 将 createTime 加入文档
},
});
};
// 自定义函数,用于将时间对象格式化成字符串
function formatTime(date) {
const year = date.getFullYear();
const month = padZero(date.getMonth() + 1);
const day = padZero(date.getDate());
const hour = padZero(date.getHours());
const minute = padZero(date.getMinutes());
const second = padZero(date.getSeconds());
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
// 自定义函数,用于补齐数字前面的零
function padZero(num) {
return num < 10 ? '0' + num : num;
}
这里简单写了一个对刚刚新建立的那个"userComments"集合进行新增的操作,需要注意的是:
这里名字不要错误
注意_id也是官方自动生成的
然后点击发布和测试,(发布即使保存代码)
此时返回刚才的userComment集合发现已经成功创建!
关于在IDE开发工具中整合云函数在上篇文章中有
3.查找
const cloud = require('@alipay/faas-server-sdk');
exports.main = async (event, context) => {
try {
const { organizationId } = event;
// 获取 cloud 环境中的 mongoDB 数据库对象
const db = cloud.database();
// 使用 where 条件查询集合对象test
const data = await db.collection('userComments')
.where({
organizationId: organizationId,
})
// 使用 get 获取文档数据
.get();
return { success: true, msg: '查询成功', data };
} catch (err) {
return { success: false, msg: `查询失败 - ${err.toString()}` };
}
};
这里不想加条件查找删掉这段即可
4.删除
将上方的.get()换成.remove()即可,然后加入where的删除条件即可
5.更改
const cloud = require('@alipay/faas-server-sdk');
const { Console } = require('console');
exports.main = async (event, context) => {
try {
const { id } = event; // 从事件参数中获取传入的 id
// 获取 cloud 环境中的 mongoDB 数据库对象
const db = cloud.database();
const oldHot = await ikun(id); // 使用 await 来等待异步函数执行完成
// 使用 where 条件查询集合对象 userComments,并更新符合条件的文档
const result = await db.collection('userComments')
.doc(id)
.update({
data: {
hot: oldHot + 1 // cloud.command.inc(1)
},
});
return { success: true, msg: '更新成功', oldHot };
} catch (err) {
return { success: false, msg: `更新失败 - ${err.toString()}` };
}
};
async function ikun(e) {
const db = cloud.database();
const result = await db.collection('userComments').where({ _id: e }).get();
return result[0].hot; // 返回查询结果中的 hot 字段值
}
6.计数
const cloud = require('@alipay/faas-server-sdk');
exports.main = async (event, context) => {
try {
const { organzitionsId } = event;
// 获取 cloud 环境中的 mongoDB 数据库对象
const db = cloud.database();
// 使用 where 条件查询集合对象 test,并获取符合条件的文档数量
const countResult = await db.collection('userComments')
.where({
organzitionsId: organzitionsId,
})
.count();
// 获取符合条件的文档数量
const count = countResult.total;
return { success: true, msg: '查询成功', count };
} catch (err) {
return { success: false, msg: `查询失败 - ${err.toString()}` };
}
};
7.一些解释
官方的规范函数写法
exports.main = async (event, context) => {}
这里的event是指代从http请求中获取其中的请求头的信息或者请求体的信息
context则包含了云函数执行的一些上下文信息,如执行环境、请求 ID 等。
其他具体查找官方文档即可