PHP MongoDB汇总$ match和$ group和$ addToSet [英] PHP MongoDB aggregate $match and $group and $addToSet
问题描述
在MongoDB中具有此文档结构:
Having this document structure in MongoDB :
{
"_id":<MongoDBID>,
"chatUser1ID": 2,
"chatUser2ID": 3
}
现在,我想从Mongo获得所有聊天伙伴,其中ID为2的聊天伙伴包含在任一"chatUser1"中.或"chatUser2".为此,我想使用$ match和$ group函数.
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'],
这会将字段chatUser1ID的所有ID放入chatUsers1集中,并且
This is putting all the ID's of the field chatUser1ID in the chatUsers1 set and
'chatUsers2' => ['$addToSet' => '$chatUser2ID']
将字段 chatUser2ID
的所有ID放入 chatUsers2
集中,其中 $ chatUserI
D要么位于文档的chatUser1ID
或 chatUser2ID
字段.
is putting all all the ID's of the field chatUser2ID
in the chatUsers2
set where the $chatUserI
D is either in the chatUser1ID
or chatUser2ID
field of the document.
之后,我想获取唯一的ID
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);
所以基本上可以正常工作,但是我想要一个解决方案,在该解决方案中,我可以直接从数据库中获取唯一ID的列表.本来我以为是线
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'],
会将ID添加到集合chatUsers中,但是不幸的是,该集合在第二行中被覆盖.有没有一种方法可以附加"将ID替换为集合,而不是覆盖它们?也许是我可以排除$ chatUserID的一种方式,因为我只需要chatPartners.
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.
提前谢谢.
推荐答案
如果您不关心它们出现的顺序,则可以构建两个数组user1和user2,然后在稍后阶段将它们连接在一起.但是,这将无法处理重复数据删除.
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'
]
]
]
],
]);
这篇关于PHP MongoDB汇总$ match和$ group和$ addToSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!