我正在尝试使用Laravel和Jens Segers(https://github.com/jenssegers/laravel-mongodb)开发的插件来检索存储在MongoDB中的嵌套文档。 我已经写了这段代码来做到这一点:
$tenant_id = \Auth::getUser()->tenant_id;
$snaps = File::raw(function($collection) use ($tenant_id)
{
return $collection->aggregate([
['$unwind' => ['path' => '$snaps']],
['$replaceRoot' => ['newRoot' => '$snaps']],
['$match' => ['tenant_id' => $tenant_id]],
]);
})->get();
foreach ($snaps as $result) {
echo $result;
}
我在MongoDB指南针中测试了这些流水线步骤,并确认它可以检索到预期的结果。 但是,运行上面的代码将产生整个指定的集合,而无需应用管道阶段。