zoukankan      html  css  js  c++  java
  • 微信小程序——聚合Aggregate操作实例中match()无法按照Date时间查询

    背景

    • 使用Date类型字段作为筛选条件时,在单表操作中很简单,使用where条件查询即可,但是在多表聚合操作中,使用match匹配Date类型字段就会报错,报错如下
      2019-11-01T03:15:08.890Z  { Error: errCode: -501007 invalid parameters | errMsg: [InvalidParameter] Check request parameter fail. Please check your request, but if the problem cannot be solved, contact us.;
      at new CloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6389:28)
      at Object.returnAsCloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6441:16)
      at Object.checkError (/var/user/node_modules/wx-server-sdk/index.js:1672:23)
      at Aggregate.<anonymous> (/var/user/node_modules/wx-server-sdk/index.js:1351:41)
      at step (/var/user/node_modules/tslib/tslib.js:136:27)
      at Object.next (/var/user/node_modules/tslib/tslib.js:117:57)
      at fulfilled (/var/user/node_modules/tslib/tslib.js:107:62)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
      errCode: -501007,
      errMsg: '[InvalidParameter] Check request parameter fail. Please check your request, but if the problem cannot be solved, contact us.; ' }
    • Next

    原因

    • 无效参数,match匹配字段为date类型时,不能直接与new Date的对象进行比较

    解决方法

    • 代码如下
      // 云函数入口文件:查询报表数据
      const cloud = require('wx-server-sdk')
      
      cloud.init()
      const db = cloud.database();
      
      
      // 云函数入口函数
      exports.main = async (event, context) => {
        const pageSize=event.pageSize;//每页数量
        const currentPage=event.currentPage;//当前页
        var startDate=event.startDate;//查询条件:开始日期
        var endDate=event.endDate;//查询条件:结束日期
        var $ = db.command.aggregate;
      
        var matchQueryObj=true;
      
        if('' != startDate && '' != endDate){
          var queryStartDate = $.dateFromString({
            dateString: new Date(startDate).toJSON()
          });
          var queryEndDate = $.dateFromString({
            dateString: new Date(endDate).toJSON()
          });
          matchQueryObj=$.and([$.gte(['$gostorage_time', queryStartDate]),$.lte(['$gostorage_time', queryEndDate])]);
      
        }else if('' != startDate && '' == endDate){
          var queryStartDate = $.dateFromString({
            dateString: new Date(startDate).toJSON()
          });
          matchQueryObj=$.gte(['$gostorage_time', queryStartDate]);
        }else if('' == startDate && '' != endDate){
          var queryEndDate = $.dateFromString({
            dateString: new Date(endDate).toJSON()
          });
          matchQueryObj=$.lte(['$gostorage_time', queryEndDate]);
        }
       
        return await db.collection('t_gostorage')
                .aggregate()
                .lookup({
                  from: "t_goods",
                  localField: "goods_id",
                  foreignField: "_id",
                  as: "goodsList"
                })
                .replaceRoot({
                  newRoot: $.mergeObjects([ $.arrayElemAt(['$goodsList', 0]), '$$ROOT' ])
                })
                .addFields({
                  matched: matchQueryObj
                })
                .match({
                  matched: true
                })
                .project({
                  goodsList: 0
                })
                .skip(pageSize*(currentPage-1))
                .limit(10)
                .end()
                .then(res =>{
                  return res;
                } )
                .catch(err => {
                  console.error(err)
                })
      
      }
    • 原理
      • addFields:在查询结果中添加一个matched的字段,字段的值为matchQueryObj的结果,值为true或者false
      • match:查找匹配结果matched=true的结果
      • 其中matchQueryObj就是时间过滤的内容,返回结果为true或者false
    • 代码注解
      • startDate和endDate是前台传过来的时间参数,分别为开始时间和结束时间
      • 据startDate和endDate是否为空组合查询条件,得到matchQueryObj
      • addFields:在查询结果中添加一个matched的字段,字段的值为matchQueryObj的结果
      • match:查找匹配结果matched=true的结果,此集合就是经过时间过滤的数据
    • Next
  • 相关阅读:
    颜色空间
    C++ lambda表达式
    nVidia的物理系统
    UE3客户端服务器GamePlay框架
    UE3中Object和Actor的创建与销毁
    git-lfs插件
    【SpringCloud】Spring Cloud Alibaba 之 Sentinel 持久化规则(三十五)
    【SpringCloud】Spring Cloud Alibaba 之 Sentinel 与OpenFeign整合(三十四)
    【SpringCloud】Spring Cloud Alibaba 之 Sentinel @SentinelResource使用(三十三)
    【SpringCloud】Spring Cloud Alibaba 之 Sentinel热点参数限流与系统自适应限流(三十二)
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/12456929.html
Copyright © 2011-2022 走看看