美文网首页
php7 mongodb 多集合查询例子

php7 mongodb 多集合查询例子

作者: AbbyLC | 来源:发表于2017-12-26 17:37 被阅读0次

自己写的,刚刚接触,可能不是最好的方案。

数据库

collection:user

/* 1 */
{
    "_id" : ObjectId("5a3d4a7d94e7d58cd34d0823"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "Mandy",
    "avatar_url" : "",
    "token" : "mandytoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

/* 2 */
{
    "_id" : ObjectId("5a3d4a9294e7d58cd34d0824"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "MengLi",
    "avatar_url" : "",
    "token" : "menglitoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

/* 3 */
{
    "_id" : ObjectId("5a3d4aae94e7d58cd34d0825"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "LuCheng",
    "avatar_url" : "",
    "token" : "lctoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

collection:vote

/* 1 */
{
    "_id" : ObjectId("5a3d4afe94e7d58cd34d0826"),
    "object_id" : "001",
    "qrcode_url" : "",
    "type" : "vote",
    "owner_token" : "mandytoken",
    "is_school" : true,
    "title" : "今天你投票了吗投票?",
    "create_time" : "",
    "start_time" : "",
    "end_time" : "",
    "question_list" : [ 
        {
            "question" : "你今天几岁啦?",
            "type" : "TYPE",
            "max_choose_num" : 1.0,
            "options" : [ 
                {
                    "option_content" : [ 
                        "YES", 
                        "NO"
                    ],
                    "selection_token_list" : [ 
                        "menglitoken", 
                        "lctoken"
                    ]
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5a3f34fb11a778666d54b005"),
    "object_id" : "002",
    "qrcode_url" : "",
    "type" : "vote",
    "owner_token" : "mandytoken",
    "is_school" : true,
    "title" : "吃饭投票",
    "create_time" : "",
    "start_time" : "",
    "end_time" : "",
    "question_list" : [ 
        {
            "question" : "什么菜?",
            "type" : "TYPE",
            "max_choose_num" : 1.0,
            "options" : [ 
                {
                    "option_content" : [ 
                        "苦瓜", 
                        "豆腐"
                    ],
                    "selection_token_list" : [ 
                        "menglitoken", 
                        "lctoken"
                    ]
                }
            ]
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5a3f368211a778666d54b009"),
    "object_id" : "003",
    "qrcode_url" : "",
    "type" : "vote",
    "owner_token" : "lctoken",
    "is_school" : true,
    "title" : "睡觉投票",
    "create_time" : "",
    "start_time" : "",
    "end_time" : "",
    "question_list" : [ 
        {
            "question" : "几点睡?",
            "type" : "TYPE",
            "max_choose_num" : 1.0,
            "options" : [ 
                {
                    "option_content" : [ 
                        "22:00", 
                        "23:00"
                    ],
                    "selection_token_list" : [ 
                        "menglitoken", 
                        "mandytoken"
                    ]
                }
            ]
        }
    ]
}

collection: selection //用户与其所投的投票活动的关系

/* 1 */
{
    "_id" : ObjectId("5a3d4a7d94e7d58cd34d0823"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "Mandy",
    "avatar_url" : "",
    "token" : "mandytoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

/* 2 */
{
    "_id" : ObjectId("5a3d4a9294e7d58cd34d0824"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "MengLi",
    "avatar_url" : "",
    "token" : "menglitoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

/* 3 */
{
    "_id" : ObjectId("5a3d4aae94e7d58cd34d0825"),
    "openid" : "",
    "session_key" : {
        "session_key" : "",
        "deadline" : ""
    },
    "unionid" : "",
    "username" : "LuCheng",
    "avatar_url" : "",
    "token" : "lctoken",
    "truename" : "",
    "school" : "",
    "school_account" : "",
    "school_password" : "",
    "major" : "",
    "dorm" : ""
}

api:获取用户参与的投票

php7 mongodb & 扩展
PSSSSSSS:以下的变量声明得有点杂乱...

GET请求,传参token
url:xxx.xxx.xxx.xxx/yyy/yyy/vote/yyy/join_list?token=menglitoken

    public function join_list()
    {
        try {
            // 拿到参数
            $token = $_GET['token'];
            // echo $token;

            // 数据库查询
            $mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017');

            // 1. 根据token去selection找object_id
            $filter = ["user_token" => $token];
            $options = [
                "projection" => ["join_time" => 1, "object_id" => 1, "_id" => 0],
            ];
            $query = new MongoDB\Driver\Query($filter, $options);
            $res1 = $mongo->executeQuery('voteDB.selection', $query);  // 对象
            // var_dump($cursor);

            // 打印 测试这阶段的操作 ——成功
            $cursor1 = $res1->toArray();  // 数组
            // cursor2  : stdClass -> Array  转换类型
            $cursor2 = json_encode($cursor1);  // json 数组
            // printf($cursor2);
            //-------------------------------------------------------------
           // 2. 根据object_id 去vote 找 time, title, fist_question
            // true 表示把其数组子对象全部decode
            $cursor3 = json_decode($cursor2, true);

            $arr = Array();
            for ($i=0; $i<count($cursor3); $i++){
                // var_dump($cursor3[$i]);
                $arr[$i] = $cursor3[$i]['object_id'];
            };
            // printf(json_encode($arr));

            $filter2 = ["object_id" =>['$in' => $arr]];
            $options2 = [
                "projection" => ["owner_token"=>1,"object_id" => 1, "title"=>1, "question_list"=>1,"_id" => 0],
            ];
            $query2 = new MongoDB\Driver\Query($filter2, $options2);
            $res2 = $mongo->executeQuery('voteDB.vote', $query2);

            $res2 = json_encode($res2->toArray());
            // printf($res2);
            // ---------------------------------------------
            // 根据owner_token 去 user 找 username
            $cursor4 = json_decode($res2, true);
            $arr1 = Array();
            for ($i=0; $i < count($cursor4); $i++) { 
                $arr1[$i] = $cursor4[$i]["owner_token"];
            };
            $filter3 = ["token" => ['$in' => $arr1]];
            $options3 = [
                "projection" => ["token" =>1,"username"=>1, "_id"=>0],
            ];
            $query3 = new MongoDB\Driver\Query($filter3, $options3);
            $res3 = $mongo->executeQuery('voteDB.user', $query3);

            $res3 = json_encode($res3->toArray());
            // printf($res3);

            $res4 = json_decode($res3, true);

            // ---------------整合---------------------------------
            $object_list = Array();
            for ($i=0; $i < count($cursor4); $i++) { 
                $object_list[$i] =Array();
                $object_list[$i]["username"] = $res4[$i]["username"];
                $object_list[$i]["title"] = $cursor4[$i]["title"];
                $object_list[$i]["time"] = $cursor3[$i]["join_time"];
                $object_list[$i]["first_question"] = $cursor4[$i]["question_list"][0]["question"];
            };
            printf(json_encode($object_list));


        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;                   //系统异常无法处理,直接向上抛出
        } catch (Exception $e) {
            $message = $e->getMessage();
            echo Json::jsonError(1, $message);    //程序内部异常输出到message中
        }

    }

相关文章

  • php7 mongodb 多集合查询例子

    自己写的,刚刚接触,可能不是最好的方案。 数据库 collection:user collection:vote ...

  • 【原创】【译】MongoDB 3.0.6查询数据

    概述 在MongoDB中,你可以用查询 find 方法来从一个集合中查询数据。MongoDB的所有查询都是单个集合...

  • MongoDB学习记录(二)

    MongoDB学习记录(二) MongoDB的查询 基本查询语句 基本的查询语句的语法:db.[集合名].fin...

  • MongoDB-第四章-查询

    查询 对MongoDB进行新增、修改和删除后,最主要的功能就是对数据(集合)进行查询,MongoDB支持丰富的查询...

  • mongo二2018-06-29

    MongoDB基础语法——查询数据 基本查询 find([{文档条件}]):全集合查询 findOne([{文档条...

  • 4.索引

    4.1 概述 索引支持在MongoDB中高效地执行查询。如果没有索引,MongoDB必须执行全集合扫描,即扫描集合...

  • mongoDB入门二

    索引 索引能够使得MongoDB更高效得执行查询。如果没有索引,MongoDB必须执行集合扫描,即扫描集合中的每个...

  • MongoDB索引与优化详解

    在MongoDB中通过建立索引可以进行高效的查询,如果没有索引MongoDB将会扫描整个集合与查询的条件进行匹配,...

  • mongodb常用操作

    多条件查询 单条件查询 排序 查看集合索引 添加单个索引 添加多个索引 查看mongodb表大小 stats() ...

  • 【mongoDB】mongoDB查询某个字段数据

    mongoDB查询某个字段数据 db.集合名.find( {}, {需要查询的字段:1, _id:0} ) 默认会...

网友评论

      本文标题:php7 mongodb 多集合查询例子

      本文链接:https://www.haomeiwen.com/subject/srujgxtx.html