Complicated Where在Laravel/MongoDB中的查询



我有一个像这样的数组(或集合)用于我的通知。每个通知都有一个数组字段对于所有看到通知的用户。

array:2 [
0 => array:8 [
"_id" => "604485ddf92e0000050079c6"
"title" => "Notification title"
"message" => "Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:2 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615103466
]
1 => array:2 [
"user_id" => "60365402546900008a007c76"
"seen_time" => "1615103466"
]
]
"updated_at" => "2021-03-07T07:50:53.314000Z"
"created_at" => "2021-03-07T07:50:53.314000Z"
]
1 => array:8 [
"_id" => "604497d2f92e0000050079c7"
"title" => "Next Notification title"
"message" => "Next Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:1 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615187929
]
]
"updated_at" => "2021-03-07T09:07:30.066000Z"
"created_at" => "2021-03-07T09:07:30.066000Z"
]
]

现在我想获得通知没有被当前用户看到(例如,使用此user_id 60365402546900008a007c76)。任何解决方案都将受到赞赏🙏。我的项目是在Laravel 8和MongoDB。

如果你想使用数组的结果,这里是一个解决方案

$userId = '60365402546900008a007c76'; $notifications = [];
foreach ($data as $notification) {
$userIds = array_column($notification['read_by_users'], 'user_id');

if (!in_array($userId, $userIds)) {
$notifications [] = $notification;
}
dd($notifications);

最新更新