Help - Search - Members - Calendar
Full Version: MongoDB fetch and sort data
HTMLHelp Forums > Programming > Server-side Scripting
sanoj96
Hello,


First time working with mongoDB within PHP.

So i am just wondering on how i should go forward with this.

So i have a database (mongodb) with a few entrys, that was given by a bot for a server i have.

What i am trying to do:

Fetch the data from the database, check if user has role "498147873003798539" (some has more then one role that is stored in an array so i will need to check the role arrays for that value, will show example of database layout).
So when i have found the useres with role "498147873003798539" i want to echo out their nickname.

Example Database Layout

CODE
    [4] => stdClass Object
        (
            [_id] => MongoDB\BSON\ObjectId Object
                (
                    [oid] => 5f0a43c4ad6d031f3441416b
                )

            [roles] => Array
                (
                    [0] => 580455589574869022
                    [1] => 605330280777252864
                )

            [username] => userNameOfUser4
            [nickname] => NickNameOfuser4
            [__v] => 0
        )

    [5] => stdClass Object
        (
            [_id] => MongoDB\BSON\ObjectId Object
                (
                    [oid] => 5f0a43c4ad6d031f34414169
                )

            [roles] => Array
                (
                    [0] => 605330280777252864
                    [1] => 224990324294942721
                    [2] => 658959318070329346
                    [3] => 659827749845991424
                    [4] => 498147873003798539
                    [5] => 643370842096664596
                )

            [username] => userNameOfUser5
            [nickname] => NickNameOfUser5
            [__v] => 0
        )


i used this php code to get that info out of the database


CODE

<?php
$filter = [];

//Manager Class
$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");

// Query Class
$query = new MongoDB\Driver\Query($filter);

// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$rows = $connection->executeQuery('dbot.datas', $query);

// Convert rows to Array and sedn result back to client
$rowsArr = $rows->toArray();

echo "<pre>".print_r($rowsArr,true)."</pre>";
?>


I have never worked with mongodb within php, so i am completly dumb when it comes to this. Have been reading around on different forums, but none of them where able to help me with this. Therefor i am making my own post now.
CharlesEF
I don't use MongoDB either. And those Mongo classes don't help either. You need to look in the PHP class itself to see what parameters each function requires and how it will return the results.

Example: Where is the SELECT statement? In MySQL that is where you define what columns you want returned, from which table, maybe a WHERE clause to limit the rows, then the sort order. MongoDB should work the same way.

Based on your code sample I 'assume' the SELECT statement should be put in the $filter variable. You would need to check the function in the class file itself to see what/how parameters are required.

If you are just learning MongoDB with PHP then I think you've made your work harder by using those classes.
sanoj96
Thanks! I didnt get it to work with mongodb, so i recreated the bot to post to mysql insted.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2021 Invision Power Services, Inc.