2

I'm using Mongodb 3.2 with PHP in Laravel with Jensseger laravel-mongodb, documentation here: https://github.com/jenssegers/laravel-mongodb

I'm inserting data through this code and it works fine:

$clientes = DB::connection(env('DB_DATABASE'))->collection('catalogo_clientes');
$clientes->insert(array("_id" => "1", "nombre" => "test", "disponible" => 1));

However, I'd like to use a function I created in mongo instead of the "1" in the "_id", when inserting through the command line I'd normally use this, which works fine:

db.loadServerScripts();
db.catalogo_clientes.insert(
    {
        _id: getNextId("clientes"),
        nombre: "Bob X.",
        disponible: 1
    }
)

How can I insert through php into mongo using the same function of "getNextId()"?

Bob Lozano
  • 838
  • 2
  • 13
  • 38
  • Possible duplicate of [MongoDB PHP Driver: Using Execute for Stored JS](http://stackoverflow.com/questions/20242361/mongodb-php-driver-using-execute-for-stored-js) – matias elgart Nov 17 '16 at 18:23
  • I'm running a function through an insert, I too saw that question but it doesn't address my issue :( – Bob Lozano Nov 17 '16 at 18:57

1 Answers1

1

This is an example using the Jenssegers' lib:

$result = DB::collection('YOUR_COLLECTION')->raw(function($collection) use ($folio, $name, $type, $entrega_digital, $motivo_rechazado)
{
    return $collection->updateOne(
    array('Folio' => (int)$folio, 'documentos_'.$type.'.nombre' => $name),
    array(
        '$set' => array('documentos_'.$type.'.$.entrega_digital' => $entrega_digital, 'documentos_'.$type.'.$.motivo_rechazado' => $motivo_rechazado)
        )
    );
});

With the cursor you can use all the native methods: https://docs.mongodb.com/manual/

Hector E.
  • 116
  • 6