收藏
回答

数据库查询返回空数组问题?

AppID
wx78cfce45531b12f3
// 云函数入口文件
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;
  // 根据deviceName选择对应的数据库集合
  const collectionName = deviceName === 'FishPond01' ? 'ali-iotXH' : 'ali-iotWB';
  // 转换startTime和endTime为数字类型时间戳
  const startTimestamp = new Date(startTime).getTime();
  const endTimestamp = new Date(endTime).getTime();
  // 分别寻找最接近startTime和endTime的记录
  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类型。


最后一次编辑于  04-10
回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    发表于移动端
    04-10
    在高级操作里面执行下查询
    04-10
    有用
    回复
登录 后发表内容