I use mongo db with php I need a last 1 hour data. I implement as like bellow.
{ "_id":{"$oid":"5ff42b30be00ec1eaf261db1"}, "logtype":"syslog", "message":"Jan 4 06:51:56 4S-096 kernel: [70745743.387001] CPU7: Package temperature above threshold, cpu clock throttled (total events = 2955852254)", "node_id":875, "app_id":0, "send_to_slack":1, "created_date":{"$date":"2021-01-05T09:02:39.593Z"} }
PHP CODE
$client = mongodb_connect(); $db = $client->$db_name; $col = $db->selectCollection($collection_name); $client->selectDatabase($db_name); $criteria = array( "created_date" => [$gte=> new MongoDBBSONUTCDateTime(strtotime("-1 hour") * 1000)], "logtype" => $logType, "message" => trim($logdata), "node_id" => $node_id, ); $count = $col->count($criteria);
I need a count result. thanks in advance
Advertisement
Answer
I am not so familiar with PHP, but I think $client->$db_name
is equal to $client->selectDatabase($db_name)
However ->
does not work with variables, so $client->$db_name
may fail.
Try this one:
$client = mongodb_connect(); $db = $client->selectDatabase($db_name); $col = $db->selectCollection($collection_name); $criteria = array( "created_date" => [ '$gte' => new MongoDBBSONUTCDateTime(strtotime("-1 hour") * 1000)], "logtype" => $logType, "message" => trim($logdata), "node_id" => $node_id, ); $count = $col->count($criteria);
I never used strtotime
, maybe you have to skip * 1000