欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

MongoDB PHP查询多维数组实现方法?

程序员文章站 2024-02-11 18:45:04
...
最近在学习Mongdb,数据结构如下,从豆瓣API拿到的数据:

在mongod里 find('{"casts.name":"杨幂"}') 是可以查询到结果的。 但是用PHP怎么都实现不了查询 2维以上多维数组里的值,求详解。

(
    [_id] => MongoId Object
        (
            [$id] => 52064a30f8c07620bad63af4
        )

    [rating] => Array
        (
            [max] => 10
            [average] => 4.8
            [stars] => 25
            [min] => 0
        )

    [reviews_count] => 313
    [wish_count] => 6308
    [collect_count] => 21907
    [douban_site] => 
    [year] => 2013
    [images] => Array
        (
            [small] => http://img3.douban.com/view/photo/icon/public/p2060736910.jpg
            [large] => http://img3.douban.com/view/movie_poster_cover/lpst/public/p2060736910.jpg
            [medium] => http://img3.douban.com/view/movie_poster_cover/spst/public/p2060736910.jpg
        )

    [alt] => http://movie.douban.com/subject/24721493/
    [id] => 24721493
    [mobile_url] => http://movie.douban.com/subject/24721493/mobile
    [title] => 小时代2:青木时代
    [do_count] => 
    [seasons_count] => 
    [schedule_url] => http://movie.douban.com/subject/24721493/cinema/
    [episodes_count] => 
    [genres] => Array
        (
            [0] => 剧情
            [1] => 喜剧
            [2] => 爱情
        )

    [countries] => Array
        (
            [0] => *
            [1] => *
        )

    [casts] => Array
        (
            [0] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img4.douban.com/img/celebrity/small/23698.jpg
                            [large] => http://img4.douban.com/img/celebrity/large/23698.jpg
                            [medium] => http://img4.douban.com/img/celebrity/medium/23698.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1052359/
                    [id] => 1052359
                    [name] => 杨幂
                )

            [1] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/39610.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/39610.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/39610.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1315611/
                    [id] => 1315611
                    [name] => 柯震东
                )

            [2] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/44400.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/44400.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/44400.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1274814/
                    [id] => 1274814
                    [name] => 郭采洁
                )

            [3] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/1372773609.01.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/1372773609.01.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/1372773609.01.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1274684/
                    [id] => 1274684
                    [name] => 凤小岳
                )

        )

    [current_season] => 
    [original_title] => 小时代2:青木时代
    [summary] => 日子一天一天的过去,林萧(杨幂 饰)、顾里(郭采洁 饰)、南湘(郭碧婷 饰)、唐宛如(谢依霖 饰)四人也迎来了大学毕业这一天,正当所有人都在为毕业之后做着规划的时候,顾里却开始着手自己的生日宴会。这本是一年一度朋友圈里最大的大事,却没想到成为顾里最不愿回想起的记忆,与此同时顾里的家庭发生了重大的变故,她也遭受到了成长以来最大的打击。而林萧、唐宛如、南湘的生活也随着大学生活的结束变得不再平稳。生活相继发生着种种让她们措手不及、不知如何面对、需要抉择的事情。姐妹四人在面对家庭、事业、爱情和友情的巨大转变下,能否依然坚持自己的生活态度?能否坚守住自己的幸福?
    [subtype] => movie
    [directors] => Array
        (
            [0] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img4.douban.com/img/celebrity/small/1372241745.58.jpg
                            [large] => http://img4.douban.com/img/celebrity/large/1372241745.58.jpg
                            [medium] => http://img4.douban.com/img/celebrity/medium/1372241745.58.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1313751/
                    [id] => 1313751
                    [name] => 郭敬明
                )

        )

    [comments_count] => 11932
    [ratings_count] => 20070
    [aka] => Array
        (
            [0] => 小时代2
            [1] => Tiny Times 2.0
        )

)

回复内容:

最近在学习Mongdb,数据结构如下,从豆瓣API拿到的数据:

在mongod里 find('{"casts.name":"杨幂"}') 是可以查询到结果的。 但是用PHP怎么都实现不了查询 2维以上多维数组里的值,求详解。

(
    [_id] => MongoId Object
        (
            [$id] => 52064a30f8c07620bad63af4
        )

    [rating] => Array
        (
            [max] => 10
            [average] => 4.8
            [stars] => 25
            [min] => 0
        )

    [reviews_count] => 313
    [wish_count] => 6308
    [collect_count] => 21907
    [douban_site] => 
    [year] => 2013
    [images] => Array
        (
            [small] => http://img3.douban.com/view/photo/icon/public/p2060736910.jpg
            [large] => http://img3.douban.com/view/movie_poster_cover/lpst/public/p2060736910.jpg
            [medium] => http://img3.douban.com/view/movie_poster_cover/spst/public/p2060736910.jpg
        )

    [alt] => http://movie.douban.com/subject/24721493/
    [id] => 24721493
    [mobile_url] => http://movie.douban.com/subject/24721493/mobile
    [title] => 小时代2:青木时代
    [do_count] => 
    [seasons_count] => 
    [schedule_url] => http://movie.douban.com/subject/24721493/cinema/
    [episodes_count] => 
    [genres] => Array
        (
            [0] => 剧情
            [1] => 喜剧
            [2] => 爱情
        )

    [countries] => Array
        (
            [0] => *
            [1] => *
        )

    [casts] => Array
        (
            [0] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img4.douban.com/img/celebrity/small/23698.jpg
                            [large] => http://img4.douban.com/img/celebrity/large/23698.jpg
                            [medium] => http://img4.douban.com/img/celebrity/medium/23698.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1052359/
                    [id] => 1052359
                    [name] => 杨幂
                )

            [1] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/39610.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/39610.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/39610.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1315611/
                    [id] => 1315611
                    [name] => 柯震东
                )

            [2] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/44400.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/44400.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/44400.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1274814/
                    [id] => 1274814
                    [name] => 郭采洁
                )

            [3] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img3.douban.com/img/celebrity/small/1372773609.01.jpg
                            [large] => http://img3.douban.com/img/celebrity/large/1372773609.01.jpg
                            [medium] => http://img3.douban.com/img/celebrity/medium/1372773609.01.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1274684/
                    [id] => 1274684
                    [name] => 凤小岳
                )

        )

    [current_season] => 
    [original_title] => 小时代2:青木时代
    [summary] => 日子一天一天的过去,林萧(杨幂 饰)、顾里(郭采洁 饰)、南湘(郭碧婷 饰)、唐宛如(谢依霖 饰)四人也迎来了大学毕业这一天,正当所有人都在为毕业之后做着规划的时候,顾里却开始着手自己的生日宴会。这本是一年一度朋友圈里最大的大事,却没想到成为顾里最不愿回想起的记忆,与此同时顾里的家庭发生了重大的变故,她也遭受到了成长以来最大的打击。而林萧、唐宛如、南湘的生活也随着大学生活的结束变得不再平稳。生活相继发生着种种让她们措手不及、不知如何面对、需要抉择的事情。姐妹四人在面对家庭、事业、爱情和友情的巨大转变下,能否依然坚持自己的生活态度?能否坚守住自己的幸福?
    [subtype] => movie
    [directors] => Array
        (
            [0] => Array
                (
                    [avatars] => Array
                        (
                            [small] => http://img4.douban.com/img/celebrity/small/1372241745.58.jpg
                            [large] => http://img4.douban.com/img/celebrity/large/1372241745.58.jpg
                            [medium] => http://img4.douban.com/img/celebrity/medium/1372241745.58.jpg
                        )

                    [alt] => http://movie.douban.com/celebrity/1313751/
                    [id] => 1313751
                    [name] => 郭敬明
                )

        )

    [comments_count] => 11932
    [ratings_count] => 20070
    [aka] => Array
        (
            [0] => 小时代2
            [1] => Tiny Times 2.0
        )

)

你的多维的查询条件是什么

// 测试这样ok
$rows = $mongo->test->test->find(array(
    'casts.name' => array('$in'=> array('杨幂','刘恺威'))
));
foreach ($rows as $row) {
    echo "{$row['title']}
"; }

请自己查一下 $elemMatch 在mongo中的用法。
是专门解决你这个问题的.

相关标签: php mongodb