php7 mongodb 多集合查询例子

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

数据库

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中
        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345