【问题标题】:Codeigniter with MongoDB sub array带有 MongoDB 子数组的 Codeigniter
【发布时间】:2012-06-06 11:30:12
【问题描述】:

我在 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

【问题讨论】:

标签: php codeigniter mongodb mongodb-php


【解决方案1】:

当查询数组中的一个元素时,你的结果会默认返回整个文档。

您可以使用 $elemMatch 返回与具有特定报告 ID 的子数组匹配的整个文档。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多