在 php 中的插入调用 Mongodb 存储函数
Calling Mongodb stored functions on an insert in php
我在 Laravel 和 Jensseger laravel-mongodb 中使用 Mongodb 3.2 和 PHP,文档在这里:https://github.com/jenssegers/laravel-mongodb
我正在通过这段代码插入数据,它工作正常:
$clientes = DB::connection(env('DB_DATABASE'))->collection('catalogo_clientes');
$clientes->insert(array("_id" => "1", "nombre" => "test", "disponible" => 1));
但是,我想使用我在 mongo 中创建的函数而不是“_id”中的“1”,当通过命令行插入时,我通常会使用它,它有效很好:
db.loadServerScripts();
db.catalogo_clientes.insert(
{
_id: getNextId("clientes"),
nombre: "Bob X.",
disponible: 1
}
)
如何使用 "getNextId()" 的相同功能将 php 插入 mongo?
这是一个使用 Jenssegers 库的示例:
$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)
)
);
});
使用光标可以使用所有本地方法:https://docs.mongodb.com/manual/
我在 Laravel 和 Jensseger laravel-mongodb 中使用 Mongodb 3.2 和 PHP,文档在这里:https://github.com/jenssegers/laravel-mongodb
我正在通过这段代码插入数据,它工作正常:
$clientes = DB::connection(env('DB_DATABASE'))->collection('catalogo_clientes');
$clientes->insert(array("_id" => "1", "nombre" => "test", "disponible" => 1));
但是,我想使用我在 mongo 中创建的函数而不是“_id”中的“1”,当通过命令行插入时,我通常会使用它,它有效很好:
db.loadServerScripts();
db.catalogo_clientes.insert(
{
_id: getNextId("clientes"),
nombre: "Bob X.",
disponible: 1
}
)
如何使用 "getNextId()" 的相同功能将 php 插入 mongo?
这是一个使用 Jenssegers 库的示例:
$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)
)
);
});
使用光标可以使用所有本地方法:https://docs.mongodb.com/manual/