首页 > 解决方案 > 将 Mongo 查询转换为 Laravel,嵌套 elemMatch

问题描述

我有一个在 Mongo 中正确运行的查询,如下所示:

db.nested.find({'path':{$elemMatch:{$elemMatch:{$in:[25]}}}})

这正确地获取了我在多维数组中寻找的数据。

但是,我正在尝试使用 laravel-mongodb (jenssegers) 库将此查询转换为在 Laravel 中使用,看来我不能在单个where().

所以做类似的事情:

DB::table('nested')->where('path','elemMatch', [25])->get();

根本不起作用,因为它在阵列下方看起来不够远。

如何正确重写第一个查询以使其正常工作?

标签: phparraysmongodblaravel

解决方案


我发现的最简单的解决方案就是whereRaw改用:

whereRaw(['path'=>['$elemMatch'=>['$elemMatch'=>['$in'=>[25]]]]])

这可以正常工作。


推荐阅读