【问题标题】:mongo db query for array inside arraymongodb查询数组内的数组
【发布时间】:2021-01-12 15:35:09
【问题描述】:

我有一个包含此类文档的集合,我需要找出所有 dispatchDateTime 小于 dispatchDetails.deliveryInformation.eventDate 的文档

{
  "trackingCode":"T1",
  dispatchDetails: [
    {
      dispatchDateTime: "2020-12-31T22:30:02.000Z",
      deliveryInformation: [
        {
          eventDate: "2020-05-15T23:46:00.000Z",
          eventCode: "E1"
        },
        {
          eventDate: "2020-05-15T23:46:00.000Z",
          eventCode: "E2"
        },
        {
          eventDate: "2020-05-15T23:46:00.000Z",
          eventCode: "E3"
        }
      ]
    }
  ]
},
{
  "trackingCode":"T2",
  dispatchDetails: [
    {
      dispatchDateTime: "2020-12-31T22:30:02.000Z",
      deliveryInformation: [
        {
          eventDate: "2020-12-31T23:46:00.000Z",
          eventCode: "E1"
        },
        {
          eventDate: "2020-12-31T23:46:00.000Z",
          eventCode: "E2"
        },
        {
          eventDate: "2020-12-31T23:46:00.000Z",
          eventCode: "E3"
        }
      ]
    }
  ]
},
{
  "trackingCode":"T3",
  dispatchDetails: [
    {
      dispatchDateTime: "2020-12-31T22:30:02.000Z",
      deliveryInformation: [
        {
          eventDate: "2020-05-12T23:46:00.000Z",
          eventCode: "E1"
        },
        {
          eventDate: "2020-05-12T23:46:00.000Z",
          eventCode: "E2"
        },
        {
          eventDate: "2020-05-12T23:46:00.000Z",
          eventCode: "E3"
        }
      ]
    }
  ]
}```

Any ideas?
In the above sample documents that query will return two documents with trackingCode T1 and T3

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    尝试以下查询:-

    db.collection.find({"dispatchDetails":{$elemMatch:{"dispatchDateTime":{$lt:"deliveryInformation.$.eventDate"}}}}})
    

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多