PHP with Mongodb - How to sign in to Mongodb and run command anyway?

Asked

Viewed 29 times

0

I created a connection with mongodb and created a session from it:

$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$sessao = $manager->startSession();
$id_sessao = $sessao->getLogicalSessionId();

In the code above I know that the session was created successfully, because if I have the $id_sessao object printed, the id is shown. But now I want to execute a "find()" in this session. That’s why I’m trying to extract the "server" to try to execute in it the command "find":

$busca_vendas = new MongoDB\Driver\Command(['find'=>'vendas','query'=>[]]);
$cursor = $sessao->getServer()->executeCommand("loja",$busca_vendas);

However, in the above code, the getServer() function returns NULL. Dai I can’t run FIND inside the session. I’m doing it right?

1 answer

0

I ended up finding another way to do it. I found that you can specify the session when performing the executionQuery(). But I still find it strange, in my previous attempt, the getServer() return empty. But in any case, so it worked:

$query = new MongoDB\Driver\Query([],[]);
$cursor = $manager->executeQuery("loja.vendas",$query,['session'=>$sessao]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.