The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> MongoDB fetch and sort data
sanoj96
post Jul 12 2020, 09:12 AM
Post #1


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
sanoj96
post Jul 18 2020, 09:30 AM
Post #2


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



Thanks! I didnt get it to work with mongodb, so i recreated the bot to post to mysql insted.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 02:04 AM