In a query I make in MongoDB using Ajax and PHP, when I'm going to manipulate ObjectId already in javascript, it returns me as [Object Object]. How do I use it as a string?
Follow the $ code .ajax ()
$.ajax({
url: 'funcoes/registroeventos.php',
data: {
"ref": ref
},
type: 'post',
dataType: 'json',
cache: false,
beforeSend: function (xhr) {
},
error: function (jqXHR, textStatus, errorThrown) {
},
success: function (dados) {
$.each(dados, function () {
$.each(this, function (index, value) {
alert(value);
});
});
}
});
Follow the PHP code:
$ref = $_POST['ref'];
try {
$consulta = ['ref' => $ref, 'excluido' => 'n'];
$opcoes = [];
$query = new MongoDB\Driver\Query($consulta, $opcoes);
$linhas = $conexao->executeQuery($bd . "maquinas", $query);
echo json_encode(iterator_to_array($linhas));
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
Example of Json
:
[
{"_id":{"$oid":"5aafac02dc32b7f93a3fda00"}, "ref":"DIP001",
"nome":"Dip Tork", "status":"setup", "excluido":"n",
"idsetor":"5aafaba2dc32b7f93a3fd9ff"}
]