How can I query MongoDB documents by a date field using Benthos?

42 Views Asked by At

Using the mongodb input in Benthos, what is the right way to query documents by a date field of type ISODate?

Here's an example of creating such a document using the MongoDB Shell (mongosh):

test> db.createCollection("dates")
{ ok: 1 }
test> db.dates.insertOne({"date": new Date()})
{
  acknowledged: true,
  insertedId: ObjectId('65c103d3540bd3cffe79bfcb')
}
test> db.dates.find()
[
  {
    _id: ObjectId('65c103d3540bd3cffe79bfcb'),
    date: ISODate('2024-02-05T15:50:43.826Z')
  }
]
1

There are 1 best solutions below

0
Mihai Todor On BEST ANSWER

The date must be passed to the query in RFC3339 format. Here is a working config which leverages the builtin Bloblang timestamp manipulation methods:

input:
  mongodb:
    url: mongodb://localhost:27017/?maxPoolSize=3
    database: dates
    collection: test
    username: "mongoadmin"
    password: "secret"
    operation: find
    json_marshal_mode: canonical
    query: |
      #!blobl

      # Exact match using the standard Golang RFC3339 layout.
      # Details here: https://pkg.go.dev/time#pkg-constants
      root.date = {"$eq": "2024-02-05T15:50:43.826Z".ts_parse("2006-01-02T15:04:05Z07:00")}

      # Alternatively, you can leverage the `ts_strptime()` method to parse a
      # time string using a more human-friendly format specifier
      # root.date = {"$gte": "2024-02-05".ts_strptime("%Y-%m-%d")}

output:
  stdout: {}