const cloud = require('wx-server-sdk');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
});
exports.main = async (event, context) => {
const db = cloud.database();
const { startTime, endTime, aiNumber, deviceName } = event;
const collectionName = deviceName === 'FishPond01' ? 'ali-iotXH' : 'ali-iotWB';
const startTimestamp = new Date(startTime).getTime();
const endTimestamp = new Date(endTime).getTime();
const startClosestRecord = await db.collection(collectionName)
.where({
time: db.command.gte(startTimestamp)
})
.orderBy('time', 'asc')
.limit(1)
.get();
const endClosestRecord = await db.collection(collectionName)
.where({
time: db.command.lte(endTimestamp)
})
.orderBy('time', 'desc')
.limit(1)
.get();
const actualStartTimestamp = startClosestRecord.data.length > 0 ? startClosestRecord.data[0].time : startTimestamp;
const actualEndTimestamp = endClosestRecord.data.length > 0 ? endClosestRecord.data[0].time : endTimestamp;
console.log(`查询时间范围: ${actualStartTimestamp} - ${actualEndTimestamp}`);
const records = await db.collection(collectionName)
.where({
time: db.command.gte(actualStartTimestamp).and(db.command.lte(actualEndTimestamp)),
})
.orderBy('time', 'asc')
.get();
console.log(`查询到的记录数: ${records.data.length}`);
let results = { Quality: [], value: [] };
records.data.forEach(record => {
console.log(`处理记录: ${record._id}`);
['Quality', 'value'].forEach(type => {
const queryField = type === 'Quality' ? `Quality${aiNumber}` : 'value';
if (record.items && record.items[`AI${aiNumber}`] && record.items[`AI${aiNumber}`][queryField] !== undefined) {
console.log(`找到匹配字段: ${queryField}`);
results[type].push({
time: record.time,
value: record.items[`AI${aiNumber}`][queryField]
});
}
});
});
console.log(`返回结果: ${JSON.stringify(results)}`);
return results;
};
附上我数据库中的数据片段:
{"_id":"2da151836615d6a50691f5db2c373e68","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:00:37","items":{"AI1":{"Quality":null,"time":1.712707236704E+12,"value":-72.5},"AI2":{"Quality":13.1,"time":1.712707236704E+12,"value":2112.03},"AI3":{"Quality":null,"time":1.712707236704E+12,"value":-45.0},"AI4":{"Quality":null,"time":1.712707236704E+12,"value":-45.0}},"productKey":"k0r3tGBYZoP"}
{"_id":"3fa9b3126615d6c3068edc9b4aa64f4b","items":{"AI4":{"value":-45.0,"Quality":null,"time":1.712707267116E+12},"AI1":{"Quality":null,"time":1.712707267116E+12,"value":-72.5},"AI2":{"Quality":13.16,"time":1.712707267116E+12,"value":2115.36},"AI3":{"Quality":null,"time":1.712707267116E+12,"value":-45.0}},"productKey":"k0r3tGBYZoP","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:01:07"}
{"_id":"3fa9b3126615d6e1068ee05423dea1ba","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:01:37","items":{"AI2":{"Quality":13.1,"time":1.71270729733E+12,"value":2112.03},"AI3":{"value":-45.0,"Quality":null,"time":1.71270729733E+12},"AI4":{"Quality":null,"time":1.71270729733E+12,"value":-45.0},"AI1":{"value":-72.5,"Quality":null,"time":1.71270729733E+12}},"productKey":"k0r3tGBYZoP"}
{"_id":"63ca5b136615d6ff0693fb641e060079","gmtCreate":"2024-04-10 08:02:07","items":{"AI2":{"time":1.712707327242E+12,"value":2108.7,"Quality":13.1},"AI3":{"Quality":null,"time":1.712707327242E+12,"value":-45.0},"AI4":{"Quality":null,"time":1.712707327242E+12,"value":-45.0},"AI1":{"Quality":null,"time":1.712707327242E+12,"value":-72.5}},"productKey":"k0r3tGBYZoP","deviceName":"FishPond01"}
{"_id":"63ca5b136615d71e0693fdc94c162e66","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:02:38","items":{"AI1":{"value":-72.5,"Quality":null,"time":1.712707357606E+12},"AI2":{"Quality":13.1,"time":1.712707357606E+12,"value":2112.03},"AI3":{"time":1.712707357606E+12,"value":-45.0,"Quality":null},"AI4":{"time":1.712707357606E+12,"value":-45.0,"Quality":null}},"productKey":"k0r3tGBYZoP"}
{"_id":"2b13da7f6615d73c068e5bf7318b8332","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:03:08","items":{"AI3":{"Quality":null,"time":1.712707387765E+12,"value":-45.0},"AI4":{"Quality":null,"time":1.712707387765E+12,"value":-45.0},"AI1":{"time":1.712707387765E+12,"value":-72.5,"Quality":null},"AI2":{"Quality":13.16,"time":1.712707387765E+12,"value":2115.36}},"productKey":"k0r3tGBYZoP"}
{"_id":"2b13da7f6615d75a068e61835f03e348","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:03:38","items":{"AI3":{"Quality":null,"time":1.712707417837E+12,"value":-45.0},"AI4":{"time":1.712707417837E+12,"value":-45.0,"Quality":null},"AI1":{"Quality":null,"time":1.712707417837E+12,"value":-72.5},"AI2":{"time":1.712707417837E+12,"value":2122.01,"Quality":13.16}},"productKey":"k0r3tGBYZoP"}
{"_id":"63ca5b136615d7790694080700360749","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:04:08","items":{"AI1":{"Quality":null,"time":1.712707448142E+12,"value":-72.5},"AI2":{"value":2115.36,"Quality":13.16,"time":1.712707448142E+12},"AI3":{"Quality":null,"time":1.712707448142E+12,"value":-45.0},"AI4":{"time":1.712707448142E+12,"value":-45.0,"Quality":null}},"productKey":"k0r3tGBYZoP"}
{"_id":"3fa9b3126615d797068ef2a43441461e","deviceName":"FishPond01","gmtCreate":"2024-04-10 08:04:38","items":{"AI4":{"Quality":null,"time":1.712707478308E+12,"value":-45.0},"AI1":{"Quality":null,"time":1.712707478308E+12,"value":-72.5},"AI2":{"Quality":13.1,"time":1.712707478308E+12,"value":2112.03},"AI3":{"Quality":null,"time":1.712707478308E+12,"value":-45.0}},"productKey":"k0r3tGBYZoP"}
以上代码执行后云函数日志得到的信息:
返回结果
{"Quality":[],"value":[]}
日志
实际情况是我数据库在所选时间段是有记录存在的而且还很多,有没有哪位大佬帮我找找这个问题原因。
补充说明一下:gmtCreate字段是string类型,time字段是number类型。