php - 带有 MongoDB 子数组的 Codeigniter

标签 php codeigniter mongodb mongodb-php

我在 mongodb 中有以下类型的集合:

Array
(
[_id] => 4fcf383a5990581c0b000015
[user_id] => 1
[username] => admin
[password] => 21232f297a57a5a743894a0e4a801fc3
[first_name] => admin
[last_name] => admin
[address] => 
[address_2] => 
 [deshboard_report] => Array
    (
        [0] => Array
            (
                [report_ids] => 1
                [rpt_color] => color-red
                [rpt_status] => max
                [report_title] => Last Point
                [report_file] => last_location
            )
        [1] => Array
            (
                [report_ids] => 2
                [rpt_color] => color-green
                [rpt_status] => max
                [report_title] => Inactive Device
                [report_file] => inactive_devices
            )
    )
)

我想在 report_ids 中搜索,搜索返回 deshboard_report 的子数组,这怎么可能?

我使用这个库连接:https://github.com/alexbilbie/codeigniter-mongodb-library

最佳答案

当查询数组中的元素时,您的结果目前默认返回整个文档。

您可以使用 $elemMatch 返回与特定 report_id 匹配的子数组的整个文档。

JS shell 的示例.. 查找 report_ids=2 的文档:

  db.collection.find({'deshboard_report': {"$elemMatch": {report_ids: 2}}});

关于php - 带有 MongoDB 子数组的 Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913340/

相关文章:

arrays - $lookup 两层

MongoDB:在多个键上执行 $inc

php - 使用ajax完成我的搜索输入

php - 在 php 中分配给关联数组切片

php - Codeigniter:发生数据库错误无法使用提供的设置连接到数据库服务器

php - Codeigniter 在 CI_Model 类中声明类级变量

php - 如何在codeigniter中从mysql获取int字段名称的值?

php - CI 中的取消链接函数抛出警告

php - 使用 CodeIgniter 和 PHP 的 get_filenames 函数内的变量

mongodb - 使用 mongoDB 切换 boolean 值