微信小程序——聚合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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM