最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

对数组$ GTE和$ LTE

运维笔记admin8浏览0评论

对数组$ GTE和$ LTE

对数组$ GTE和$ LTE

我有模式

{ summaries:
  [
   {
     type: 'temp',
     min: 10,
     max: 40
   },
   {
     type: 'humidity',
     min: 3,
     max: 40
   }
  ],
  thresholds: [
  {
    type: 'temp',
    low: -10,
    high: 40
  },
  {
    type: 'humidity',
    low: 9,
    high: 30
  }
 ]
}

上面的这个对象是集合的一个记录。我想记录,如果summaries.type ==“临时”和thresholds.type ==“临时”和summaries.min> thresholds.low

摘要对象与阈值比较的对象必须是同一类型。温度与温度相比,哼哼比较嗡嗡声。有任何对象摘要温度那分钟<任何阈值对象温度将被返回

例:

{ summaries:
  [
   {
     type: 'temp',
     min: 10,
     max: 40
   },
   {
     type: 'humidity',
     min: 3,
     max: 40
   }
  ],
  thresholds: [
  {
    type: 'temp',
    low: -10,
    high: 40
  },
  {
    type: 'humidity',
    low: 9,
    high: 30
  }
 ]
}

将返回临时分钟= 10,阈值低= -10。

{ summaries:
  [
   {
     type: 'temp',
     min: 10,
     max: 40
   },
   {
     type: 'humidity',
     min: 3,
     max: 40
   }
  ],
  thresholds: [
  {
    type: 'temp',
    low: 20,
    high: 40
  },
  {
    type: 'humidity',
    low: 9,
    high: 30
  }
 ]
}

将因为被不返回临时分钟= 10,阈值温度低= 20尽管阈值低哼声= 9

回答如下:

您可以使用$expr使用下面的查询

db.collection.find({
  "$expr": {
    "$gt": [
      {
        "$arrayElemAt": [
          "$summaries.min",
          { "$indexOfArray": ["$summaries.type", "temp"] }
        ]
      },
      {
        "$arrayElemAt": [
          "$thresholds.low",
          { "$indexOfArray": ["$thresholds.type", "temp"] }
        ]
      }
    ]
  }
})

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论