我有一个MongoDB集合,其中包含以下格式的文档 集合名称称为帖子
{
"_id" : ObjectId("5c88b225fac24431d947abad"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"post_likes" : [
{
"post_user_id" : "5c87ad6c623f1e2bd4d041d0",
"like_status" : true
},
{
"post_user_id" : "5c88b42b71611926c055508b",
"like_status" : true
}
],
"post_comments" : [
{
"comment_user_id" : "5c87ad6c623f1e2bd4d041d0",
"comment_like" : "",
"comment_description" : ""
},
{
"comment_user_id" : "5c88b42b71611926c055508b",
"comment_like" : "",
"comment_description" : "nice post"
}
]
}
我还有一个名为user_ptofile_info的集合名称
{
"_id" : ObjectId("5c923682c088564cf01056cb"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"image_url" : "image/url",
"user_name":"xxxxxxxx",
"created_at" : "",
"updated_at" : ""
}
要求的输出,如
注意:user_ptofile_info中的post_user_id和post中的user_id是查找对象,我也需要post_likes和发贴评论的总数
{
"_id" : ObjectId("5c88b225fac24431d947abad"),
"user_id" : "5c87ad6c623f1e2bd4d041d0",
"post_likes" : [
{
"post_user_id" : "5c87ad6c623f1e2bd4d041d0",
"like_status" : true,
"image_url" : "image/url",
"user_name":"xxxxxxxx",
},
{
"post_user_id" : "5c88b42b71611926c055508b",
"like_status" : true,
"image_url" : "image/url",
"user_name":"xxxxxxxx",
}
],
"post_comments" : [
{
"comment_user_id" : "5c87ad6c623f1e2bd4d041d0",
"comment_like" : "",
"comment_description" : ""
},
{
"comment_user_id" : "5c88b42b71611926c055508b",
"comment_like" : "",
"comment_description" : "nice post"
}
]
}
答案 0 :(得分:1)
您可以使用以下汇总:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}
$lookup with custom pipeline(MongoDB 3.6或更高版本)将允许您从question = open("questions.txt", "r")
print(question.readlines()[0])
tanswer = open("answers.txt", "r")
correct_answer = float(tanswer.readlines()[0])
uanswer = float(input("Write the answer: "))
if correct_answer==uanswer:
print("Amazing " + str(correct_answer) + " is the correct answer")
else:
print("Wrong " + str(uanswer) + " is not correct, Try again please. ")
获取db.post.aggregate([
{
$lookup: {
from: "user_profile_info",
let: { user_ids: "$post_likes.post_user_id" },
pipeline: [
{ $match: { $expr: { $in: [ "$user_id", "$$user_ids" ] } } },
{
$project: {
post_user_id: "$user_id",
image_url: 1,
user_name: 1
}
}
],
as: "users"
}
},
{
$project: {
_id: 1,
user_id: 1,
post_likes: {
$map: {
input: "$users",
as: "user",
in: {
post_user_id: "$$user.post_user_id",
image_url: "$$user.image_url",
user_name: "$$user.user_name",
like_status: {
$let: {
vars: {
like: {
$arrayElemAt: [
{ $filter: { input: "$post_likes", as: "pl", cond: { $eq: [ "$$pl.post_user_id", "$$user.post_user_id" ] } } }, 0
]
}
},
in: "$$like.like_status"
}
}
}
}
},
post_comments: 1
}
}
])
数组中存在的所有用户的数据。然后,您需要将user_profile_info
的{{1}}数组与post_likes
合并以获取users
。由于您有两个数组,并且您知道两个数组中都出现相同的post_likes
,因此可以将$map与$arrayElemAt和$filter结合使用,以合并来自两个数组的数据。 / p>