Count MongoDB Logs Errors on a Remote Server

Viewed 45

I'm Trying run to the following query (Custom Count of the Errors in logs):

db.getCollection('logs').count({
    "timestamp" : {$gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 1)},
    "level" : "error",
    "message" : {$not: {$regex:  ".*sso"}}
    })

On a Remote MongoDB Cluster, using a certificate based authentication.

Typicality,
I will use the --eval flag to call a .js script.

But due to a security reasons, i can't save files locally.

So I tried using the following syntax combinations:

1. echo -e '':

echo -e 'db.getCollection('logs').count({"timestamp" : {$gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 1)},"level" : "error","message" : {$not: {$regex:  ".*sso"}}})' | mongo --ssl --sslPEMKeyFile /mongodb/certificate/test.pem --sslCAFile /mongodb/certificate/testCA.pem  --authenticationDatabase '$external' --authenticationMechanism MONGODB-X509 --host MyServer.MyDomain --quiet --port 27019

2. echo $'':

echo $'db.getCollection('logs').count({"timestamp" : {$gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 1)},"level" : "error","message" : {$not: {$regex:  ".*sso"}}})' | mongo --ssl --sslPEMKeyFile /mongodb/certificate/test.pem --sslCAFile /mongodb/certificate/testCA.pem  --authenticationDatabase '$external' --authenticationMechanism MONGODB-X509 --host MyServer.MyDomain --quiet --port 27019

3. printf '':

printf 'db.getCollection('logs').count({"timestamp" : {$gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 1)},"level" : "error","message" : {$not: {$regex:  ".*sso"}}})' | mongo --ssl --sslPEMKeyFile /mongodb/certificate/test.pem --sslCAFile /mongodb/certificate/testCA.pem  --authenticationDatabase '$external' --authenticationMechanism MONGODB-X509 --host MyServer.MyDomain --quiet --port 27019

To pass the query, to the mongo shell command.

But i'm getting the same error, each time, that logs is not defined :

2021-10-25T17:06:04.260+0300 E QUERY    [thread1] ReferenceError: logs is not defined :
@(shell):1:1

Not sure exactly what is the problem, it seems that i need to escape some charter. but i didn't find any documentation.

0 Answers
Related