php - 将 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();
根本不起作用,因为它在阵列下方看起来不够远。
如何正确重写第一个查询以使其正常工作?
解决方案
我发现的最简单的解决方案就是whereRaw
改用:
whereRaw(['path'=>['$elemMatch'=>['$elemMatch'=>['$in'=>[25]]]]])
这可以正常工作。
推荐阅读
- android - Queue Ble Android 仅准时发送
- reactjs - 模块解析失败:意外的令牌?
- vue.js - Eslint 会因 Vue 项目中的任何错误而中断构建
- powershell - ImageMagick @filename 引用多个输出
- java - 存储多个文本字段输入
- arrays - 尝试删除数组中的元素:Ruby
- python - PSET6 DNA:代码仅计算一个 STR 序列的最长连续重复次数
- angular - 如何从我的应用程序内的项目中获取资产文件?
- r - 在ggplot2中删除或合并标题并调整距离?
- javascript - 使用ajax同步隐形recaptcha