Node.js MongoDB/Mongoose在特定日期之间查询文档?

Node.js MongoDB/Mongoose在特定日期之间查询文档?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我想让它在特定日期之间获取文档,我知道我需要使用$和操作符,但现在我使用的是MongoDB的中间件,我阅读了文档,但找不到与之相关的任何内容 我现在做的是: router.get('/', token({ required: true }), query({ after: { type: Date, paths: ['fecha'], operator: '$gte' }, before: { type: Date,

我想让它在特定日期之间获取文档,我知道我需要使用$和操作符,但现在我使用的是MongoDB的中间件,我阅读了文档,但找不到与之相关的任何内容

我现在做的是:

router.get('/',
  token({ required: true }),
  query({
    after: {
      type: Date,
      paths: ['fecha'],
      operator: '$gte'
    },
    before: {
      type: Date,
      paths: ['fecha'],
      operator: '$lte'
    }
  }),
  index)

因此,我的问题是如何使用$and运算符,在查询中不必使用
$and
运算符。与此相反:

{
  $and: [
    { yourDateField: { $lte: '2018-08-09' } },
    { yourDateField: { $gte: '2018-08-03' } },
  ]
}
你可以做:

{
  yourDateField: {
    $lte: '2018-08-09',
    $gte: '2018-08-03'
  }
}
因此,您可以将此模式用于querymen

const querySchema = {
  after: {
    type: Date,
    paths: ['yourDateField'],
    operator: '$gte'
  },
  before: {
    type: Date,
    paths: ['yourDateField'],
    operator: '$lte'
  }
};
完整示例可能如下所示:

const querymen = require('querymen');
const app = require('express')();

const querySchema = {
  after: {
    type: Date,
    paths: ['yourDateField'],
    operator: '$gte'
  },
  before: {
    type: Date,
    paths: ['yourDateField'],
    operator: '$lte'
  }
};

app.get('/', querymen.middleware(querySchema), (req, res) => {
  const { querymen: { query } } = req;
  res.json({ ok: true, query });
});

app.listen(8000, () => console.log('Up and running on http://localhost:8000'));

如果您仍有疑问,为什么不使用
$和
运算符查看。

谢谢,我用不同的方法解决了它,但它很混乱,您的解决方案更优雅,而且我不知道不需要使用$和运算符