The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> MongoDB fetch and sort data
sanoj96
post Jul 12 2020, 09:12 AM
Post #1


Advanced Member
****

Group: Members
Posts: 114
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
CharlesEF
post Jul 12 2020, 02:57 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,746
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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


Advanced Member
****

Group: Members
Posts: 114
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

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

 



- Lo-Fi Version Time is now: 3rd August 2020 - 01:10 PM