Javascript mongoose中$in的动态变量

Javascript mongoose中$in的动态变量,javascript,node.js,mongodb,mongoose,mongodb-query,Javascript,Node.js,Mongodb,Mongoose,Mongodb Query,我有以下变量。(我正在mongoose节点中使用此查询) 我希望它出现在mongoDB查询中: { batchRef: { $in: [/20191209/, /20191210/] } }, // this is static values and works. { batchRef: { $in: ['/'+previousDate+'/','/'+currentDate+'/'] } }, // this doesnt work! 我错过什么了吗? 任何帮助都将不胜感激。这看起来像是一个

我有以下变量。(我正在mongoose节点中使用此查询)

我希望它出现在mongoDB查询中:

{ batchRef: { $in: [/20191209/, /20191210/] } }, // this is static values and works.
{ batchRef: { $in: ['/'+previousDate+'/','/'+currentDate+'/'] } }, // this doesnt work!
我错过什么了吗?
任何帮助都将不胜感激。

这看起来像是一个javascript日期转换问题

您可以使用以下代码获取给定日期和上一个日期的字符串数组,以便在$in查询中使用

const inputDateString = "2019-12-09";

const currentDate = new Date(inputDateString);
const previousDate = new Date(inputDateString);
previousDate.setDate(currentDate.getDate() - 1);

let inArray = [];

inArray.push(inputDateString.split("-").join(""));
inArray.push(
  previousDate
    .toISOString()
    .slice(0, 10)
    .split("-")
    .join("")
);

console.log(inArray);
inArray将有以下两项:

[ '20191209', '20191208' ]
现在,我们可以在如下查询中使用:

  { batchRef: { $in: inArray } }
请不要以为我假设inputDateString总是采用这种格式,有10个字符,比如
2019-01-31
2019-01-01

  { batchRef: { $in: inArray } }