Want to use function and execute the below query in script.

#!/bin/bash
function getDeliveredDispatches(firstDateTime, lastDateTime, limit) {
var cursor = db.dispatches.find(
{$and: [
{"createdDtm" : {$gte : firstDateTime, $lte:lastDateTime}},
{"status.code" : {$in : ["DELIVERY_PENDING", "DELIVERY_SUCCESSFUL"]}}
]},
{"deliveryGroupId" : 1, "batchId": 1, "statusHistory.code" : 1}
);
var wrongDispatchesIds = [];
print("Number of dispatches selected based on filter = " + cursor.size())
while (cursor.hasNext()) {
var dispatch = cursor.next();
var index_generation_successfull = dispatch.statusHistory.map(function (status) {return status.code}).indexOf("GENERATION_SUCCESSFUL");
var index_delivery_pending = dispatch.statusHistory.map(function (status) {return status.code}).indexOf("DELIVERY_PENDING");

I have the below query which fetches some data from db. Whenever I am trying to execute it in bash I get: syntax error near unexpected token `firstDateTime,'

Kindly assist where am I going wrong. It would be a great help.

Welcome to Forums

This might help you

Bash function syntax

function function_name {
   your commands goes here...
}
function_name () {
   your commands goes here...
}

To call function

function_name $argument1 $argument2

Example :

$ cat tester
#!/bin/bash
foo() {
    echo "First Argument is $1"
    echo "Second Argument is $2"
}

foo argument_1 argument_2 

$ bash tester
First Argument is argument_1
Second Argument is argument_2

Also, read up on bash syntax first, since there is virtually no discernible bash code in your snippet!