Heim > Backend-Entwicklung > PHP-Tutorial > MongoDB PHP查询多维数组实现方法?

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

WBOY
Freigeben: 2016-06-06 20:49:33
Original
1275 Leute haben es durchsucht

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

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

<code>(
    [_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
        )

)

</code>
Nach dem Login kopieren
Nach dem Login kopieren

回复内容:

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

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

<code>(
    [_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
        )

)

</code>
Nach dem Login kopieren
Nach dem Login kopieren

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

<code class="lang-php">// 测试这样ok
$rows = $mongo->test->test->find(array(
    'casts.name' => array('$in'=> array('杨幂','刘恺威'))
));
foreach ($rows as $row) {
    echo "{$row['title']}<br>";
}
</code>
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage