Php Mongo how to pass a variable to a cursor

I am unfamiliar with the syntax to pass a variable to a mongo cursor. Currently I have the ‘name’ hardcoded as ‘ABC’. But I want to be able to pass a variable instead of hardcoding ‘ABC’.

$client = new MongoDB\Client(“mongodb://localhost”);
$dbs = $client ->database_name->table_name; // $client → DBNAME → COLLECTION_NAME
$result = $dbs → find(array(‘name’ => ‘ABC’));

I tried this, but it is not working.

$name = ‘ABC’;
$result = $dbs → find(array(‘name’ => $name));

Can someone please provide me with the correct syntax? Thanks

Hello @Andrew_Manager and welcome to the MongoDB Community Forum!

If I understand correctly, the hard-coded version works, but the one with the variable does not. I would suggest initializing your array outside of the find() function and passing the array as a variable.
That will give you a chance to verify the array was initialized as you expected by either looking with a debugger or doing a var_dump ()

Let us know!
Hubert