PHP MongoDB aggregate $match and $group and $addToSet

我怕爱的太早我们不能终老 提交于 2020-12-14 23:37:15

问题


Having this document structure in MongoDB :

{
"_id":<MongoDBID>,
"chatUser1ID": 2,
"chatUser2ID": 3
}

Now i want to get all chat partners from Mongo where the Chat Partner with the ID 2 is included in either "chatUser1" or "chatUser2". For that i want to use the $match and $group function.

$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
            [
                '$match'  => 
                    [
                        '$or' => 
                            [
                                ["chatUser1ID" => $chatUserID],
                                ["chatUser2ID" => $chatUserID]
                            ]
                    ]
            ]
            ,[
                '$group' => 
                    [
                        '_id' => 0,
                        'chatUsers' => ['$addToSet' => '$chatUser1ID'],
                        'chatUsers' => ['$addToSet' => '$chatUser2ID'],
                        'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
                        'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
                    ]
            ]
        ]);
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],

This is putting all the ID's of the field chatUser1ID in the chatUsers1 set and

'chatUsers2' => ['$addToSet' => '$chatUser2ID']

is putting all all the ID's of the field chatUser2ID in the chatUsers2 set where the $chatUserID is either in the chatUser1ID or chatUser2ID field of the document.

After that i want want to get the unique ID's

$chatUserIDs = array();
foreach ($chatCursor as $counter => $document) {
    $bson = MongoDB\BSON\fromPHP($document);
    $value = MongoDB\BSON\toJSON($bson);
    $value = json_decode($value, true);
    $chatUserIDs = array_unique(array_merge($value['chatUsers1'], $value['chatUsers2']));
}
unset($chatUserIDs[array_search($chatUserID, $chatUserIDs)]);
array_unshift($chatUserIDs);

So basically it's working but i want the solution where i get a list of unique ID's right away from the database. Originally i thought the lines

'chatUsers' => ['$addToSet' => '$chatUser1ID'],
'chatUsers' => ['$addToSet' => '$chatUser2ID'],

would add the ID's to the set chatUsers but unfortunately the set is overwritten in the second line. Is there a way to "append" the ID's to the set instead of overwrite them ? And maybe a way where i can exclude the $chatUserID because i want only the chatPartners.

Thanks in advance.


回答1:


If you don't care about the order they appear in, you can build two arrays of user1 and user2, then concat them together in a later stage. This won't handle deduplicating though.

$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
    [
        '$match' => [
            '$or' =>[
                ["chatUser1ID" => $chatUserID],
                ["chatUser2ID" => $chatUserID]
            ]
        ]
    ], [
        '$group' => [
            '_id' => 0,
            'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
            'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
        ]
    ], [
        '$addFields' => [
            'chatUsers' => [
                 '$concatArrays' => [
                     '$chatUsers1',
                     '$chatUsers2'
                 ]
            ]
         ]
    ],
]);


来源:https://stackoverflow.com/questions/64960899/php-mongodb-aggregate-match-and-group-and-addtoset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!