Find document range same day from mongodb use GetMongo processor by NiFi

135 views Asked by At

I want to get the documents in mongodb for the day. I tried querying with mongo compass the code works. The code is as follows:

{
  "time_created": {
    "$gte": new Date(new Date().setHours(0, 0, 0, 0)),
    "$lt": new Date(new Date().setHours(23, 59, 59, 999))
  }
}

But when I use the above query code with the "query" property of GetMongo, I get the following error message:

Verification Results
Perform Validation
Component is invalid: 'Query' validated against '{ "time_created": { "$gte": new Date(new Date().setHours(0, 0, 0, 0)), "$lt": new Date(new Date().setHours(23, 59, 59, 999)) } }' is invalid because Query is not a valid JSON representation due to Unrecognized token 'new': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (String)"{ "time_created": { "$gte": new Date(new Date().setHours(0, 0, 0, 0)), "$lt": new Date(new Date().setHours(23, 59, 59, 999)) } }"; line: 3, column: 16]

enter image description here

enter image description here

I tried converting to JSON format but it doesn't work:

{
    "time_created": {
    "$gte": { "$date": "${now():format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'')}" },
    "$lt": { "$date": "${now():format('yyyy-MM-dd\'T\'23:59:59.999\'Z\'')}" }
    }
}

and

{ 
    "time_created": { 
        "$gte": "${now():toDate():format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'')}",
    }
}

but it not work.

Please help me, thanks.

Can you help me convert mongodb query to json format working with GetMongo ?

{
  "time_created": {
    "$gte": new Date(new Date().setHours(0, 0, 0, 0)),
    "$lt": new Date(new Date().setHours(23, 59, 59, 999))
  }
}

Give me another more suitable solution ?

Please help me, thanks.

1

There are 1 answers

0
Manh Quang Do On

I fixed it. Nifi's getMongo Query field doesnt support EL. So i created a stored function in MongoDB for my dynamic query and called it from Nifi.

Create function in Mongodb:

db.system.js.insertOne({
   _id: "find_data_by_date_range",
   value : function(created_time) {
    var fromDate = new Date(new Date().setHours(0, 0, 0, 0));
    var toDate = new Date(new Date().setHours(23, 59, 59, 999));
    if ((created_time >= fromDate) && (created_time <= toDate)) {
      return true;
    }
    return false;
   }
});

Query Nifi:

{"$where": "find_data_by_date_range(this.time_created)"}

It is worked!