1

If I have the following schema:

users = {
  _id: MongoID
  name: String,
  email: String,
  password: String,
  online: Boolean,       // Tells if user is online or not
  friends: Array,        // Storing _ids of friends
  request: Array
}

Now, I want to get the name of all the friends which are online, sorted by name.

MongoDB database:

{
    "_id" : ObjectId("572e43d71ccbdd080f00002b"),
    "name" : "Bot 3",
    "email" : "[email protected]",
    "password" : "202cb962ac59075b964b07152d234b70",
    "online" : false,
    "friends" : [
        ObjectId("572efe481ccbdd9c12000029")
    ],
    "requests" : []
}
{
    "_id" : ObjectId("572efe481ccbdd9c12000029"),
    "name" : "Bot 4",
    "email" : "[email protected]",
    "password" : "202cb962ac59075b964b07152d234b70",
    "online" : false,
    "friends" : [
        ObjectId("573074e81ccbddc816000029"),
        ObjectId("572e43d71ccbdd080f00002b")
    ],
    "requests" : [ ]
}
{
    "_id" : ObjectId("573074e81ccbddc816000029"),
    "name" : "Bot 5",
    "email" : "[email protected]",
    "password" : "202cb962ac59075b964b07152d234b70",
    "online" : true,
    "friends" : [
        ObjectId("572efe481ccbdd9c12000029")
    ],
    "requests" : [ ]
}

I want some query like:

var arr = db.users.find({"_id": ObjectId("572efe481ccbdd9c12000029")},
          {"friends": 1, "_id": 0});

db.users.find({$in: arr["friends"]}, {"name": 1, "online": 1}).sort({"name": 1});

I tried this php code on the above example (not even close to requirement):

<?php
    $cursor1 = $users->find(
        array(
            "_id" => new MongoID($id),
        ),
        array(
            "friends" => 1
        )
    );
    $cursor1->next();
    $doc1 = $cursor1->current();
    $friend_arr = $doc1['friends'];
    foreach($friend_arr as $f_id){
        $cursor2 = $users->find(
            array("_id" => new MongoID($f_id)),
            array("name"=>1));
        $cursor2->next();
        $doc2 = $cursor2->current();
        echo "<div>".$doc2['name']."</div>";
    }

?>

But I know it does not satisfy the needs. That's why I am asking for help.

I am new to MongoDB with php.

Thank you so much in advance for any help.

0

1 Answer 1

2

Ok guys, I got the solution after some thinking.

$cursor1 = $users->find(
array(
    "_id" => new MongoID($id),
),
array(
    "friends" => 1
)
);
$cursor1->next();
$doc1 = $cursor1->current();
$friend_arr = $doc1['friends'];

$cursor2 = $users->find(
    array(
        "_id"=> array(
            '$in' => $friend_arr
        )
    ),
    array(
        "name" => 1
    )
);
$cursor2->sort(array("name"=>1));


foreach($cursor2 as $doc2){
    echo "<div>".$doc2['name']."</div>";
}

This code works properly. I tested it before posting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.