【问题标题】:Mongoose - Query with $gt combined with $size not workingMongoose - 使用 $gt 和 $size 的查询不起作用
【发布时间】:2022-02-21 18:16:02
【问题描述】:

这是我的猫鼬查询(和路由器):

router.get('/reportsRegular', function(req,res,next){
  Question.find({reports: {$size: {$gt: 0}}, checked: false}).sort({reports: -1}).limit(20).exec(function(err,results){
    console.log(results)
    res.render('reports', {type: 'regular', user: req.user, reports: results})
})

第一个查找条件似乎有问题,当我将 $gt 删除为 1 时,它可以工作,但它不会在多个情况下工作,所以我需要使用 $gt。

这是一个应该可以工作但找不到的示例 JSON 文档:

{
  _id: new ObjectId("6212e77aa1e98ae3282a61e6"),
  title: 'TOMATOOOOOO',
  text: '<p>AAAAAAAAAAAAAAAA</p>',
  authorUsername: 'SweetWhite',
  dateCreated: 2022-02-21T01:14:34.901Z,
  answers: [],
  likes: [ 0 ],
  dislikes: [ 0 ],
  tag: 'Languages',
  views: [ 1, 'SweetWhite' ],
  reports: [ 'SweetWhite' ],
  checked: false,
  reportsNested: [],
  __v: 0
}

它应该被找到,因为报告数组的大小大于零,并且检查的值为 false。 我做错了什么?

谢谢!

【问题讨论】:

    标签: javascript json mongodb mongoose mongodb-query


    【解决方案1】:

    您需要$expr。并将过滤器更改为:

    {
      $expr: {
        $gt: [
          {
            "$size": "$reports"
          },
          0
        ]
      },
      checked: false
    }
    

    Sample Mongo Playground

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 2017-02-08
      • 2017-01-19
      • 1970-01-01
      • 2019-08-24
      • 2019-04-29
      • 2021-04-26
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多