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()"?

2
  • Possible duplicate of MongoDB PHP Driver: Using Execute for Stored JS Commented Nov 17, 2016 at 18:23
  • I'm running a function through an insert, I too saw that question but it doesn't address my issue :( Commented Nov 17, 2016 at 18:57

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.