查询以查找所有非零毫秒的文档 [英] Query to find all documents with non-zero milliseconds

查看:21
本文介绍了查询以查找所有非零毫秒的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有大量transaction 文档(~2M)

There is a large collection of transaction documents (~2M)

每个transaction 文档都有一个source.billDate 字段:

Each transaction document has a source.billDate field:

"source.billDate" : ISODate("2018-07-23T16:02:06.797Z")
// or //
"source.billDate" : ISODate("2018-07-22T14:21:56.000Z")

如您所见,有些日期有毫秒,有些则没有.

我想知道是否有一种方法可以使用 MongoDB 查询来查找 billDate 日期中包含毫秒的 transaction 文档.

I'm wondering if there is a way to use MongoDB query to find transaction documents that have milliseconds in their billDate date.

有可能吗,如果有,怎么做?

Is it possible, if so, how?

推荐答案

你可以在mongodb3.6版本中尝试以下查询

You can try below queries in mongodb 3.6 version

您必须首先使用 $dateToParts 然后您可以轻松匹配具有毫秒 $ne 0

You have to first extract millisecond from your date using $dateToParts and then you can easily match with the documents having millisecond $ne 0

db.collection.aggregate([
  { "$match": {
    "$expr": {
      "$ne": [
        { "$millisecond": {
          "date": "$source.billDate",
          "timezone": "America/New_York"
        }},
        0
      ]
    }
  }}
])

或者也可以使用 find 查询

Or with find query as well

db.collection.find({
  "$expr": {
    "$ne": [
      { "$millisecond": {
        "date": "$source.billDate",
        "timezone": "America/New_York"
      }},
      0
    ]
  }
})

这篇关于查询以查找所有非零毫秒的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆