node.js - 如何通过 mongodb nodejs 的平均评分加入产品来获得价值?
问题描述
参数:-
"access_token": "weZVy7pnlV6CtnSQ5w8ZO9iatr5/RZrxc8vc0R5G/kyIiBhfloQuwTIVPIxUupheKHdjhpCjL2g+PsfUPe79cA==",
"api_key": "T0BAnEzf6CaWwIMulImtA05nT6fDVjXKHoJM136S",
"search_key": "",
"latitude": 22.723755,
"longitude": 75.886752,
"maximum_distance": 5000,
"page": 1,
"limit": 5000
我想要的是:-
:---- 我需要所有组的距离数据(基于搜索的经纬度)、平均评分、rating_status、join_status。
:---- SORT BY 距离同意和平均评级下降。
:---- 用户评分状态(如果用户已经获得评分,那么 rating_status 将为是/否),
:---- 组加入状态(如果用户已经加入组,则 join_status 将为是/否) .
:---- 按组名搜索。(如果搜索键可用)。
评论和评级表 集合结构:
表名 = 评论。
var reviewSchema = new Schema({
user_id : { type: String, required: true, default: "" },
room_id : { type: String, required: true, default: "" },
rating : { type: Number, min: 1, max: 5, require: true, default: "" },
comment: { type: String, require: true, default: "" },
created_at : { type: String, required: true, default: "" },
updated_at : { type: String, default: "" }
});
和表格内容这种类型的数据:-
"data": [
{
"_id": "5e3a6f5439217717b0ed56de", // review table primary id
"user_id": "5e2ad43ec2a9ae09e8ccefb1"
"room_id": "5e3a5dc4cc182919340dc066", //room id or group id
"rating": 5,
"comment": "Good",
"created_at": "1580887892400",
"updated_at": "1580887892400",
"__v": 0,
},
{
"_id": "5e3ac1dffc459005689c8e25",
"user_id": "5e3a93d9034f591a9064195f"
"room_id": "5e3a5dc4cc182919340dc066",
"rating": 2,
"comment": "Good comment",
"created_at": "1580909023395",
"updated_at": "1580909023395",
"__v": 0,
}
]
这是房间的桌子结构
var RoomSchema = new Schema({
name : {type: String, required: true, default: ""},
icon_url : {type: String, default: ""}, // group icon
created_by : {type: Object, required: true, default: ""}, // user id who created this group
users: [{
user_id : {type : Object, default: ""},
is_admin : {type : Boolean, default: false},
status: {type: String, default: "joined"},
join_at: {type: String, required: true},
display_name : {type: String, default: ""}
}],
invitations: [{ //new added
sender_id : {type : Object, default: ""},
receiver_id : {type : Object, default: ""}
}],
open_to_all : {type: Boolean, default:false},
created_at : {type: String, required: true},
updated_at : {type: String, required: true}, //new added
is_group : {type : Boolean, default : true},
history_enable : {type : Boolean, default : true},
invitation_blocked : {type : Boolean, default : true},
is_deleted : {type : Boolean, default: false},
group_address: {type: String, default: ""},
keyword : {type : String, required: true, default: ""}, //new added
location : {
type : {type : Object, default: "Point"},
coordinates : {type : Array, required: true, default: ""}
}});
和表格内容这种类型的数据: -
{
"data": [
{
"_id": "5e3a5dc4cc182919340dc066", //Room id or group id
"created_at": "1580883396150",
"updated_at": "1580911250917",
"location": {
"type": "Point",
"coordinates": [
22.724515,
75.884
]
},
"keyword": "burhan",
"group_address": "Testss",
"is_deleted": false,
"invitation_blocked": true,
"history_enable": true,
"is_group": true,
"open_to_all": false,
"invitations": [],
"users": [
{
"join_at": "1580883396150",
"_id": "5e3a5dc4cc182919340dc067", //user id
"display_name": "Navlakha square 44",
"status": "joined",
"is_admin": true,
"user_id": "5e2ad43ec2a9ae09e8ccefb1"
},
{
"user_id": "5e3a93d9034f591a9064195f", //user id
"is_admin": false,
"status": "joined",
"display_name": "",
"_id": "5e3a9a8065746703c0d126b7",
"join_at": "1580905781807"
}
],
"created_by": [
{
"_id": "5e2ad43ec2a9ae09e8ccefb1",
"created_at": "1579865150137",
"last_login": "1579865150137",
"blocks": [],
"blocked": [],
"muted_room": [],
"blocked_room": [],
"categories": [],
"invitations": [],
"rooms": [
"5b27477e4b20cb8fab0d6b3a",
"5e3a5dc4cc182919340dc066",
"5e3a72b1c38a3819b89db2b7"
],
"is_blocked": false,
"is_login": true,
"online_status": true,
"socket_id": "",
"access_token": "weZVy7pnlV6CtnSQ5w8ZO9iatr5/RZrxc8vc0R5G/kyIiBhfloQuwTIVPIxUupheKHdjhpCjL2g+PsfUPe79cA==",
"notification_token": "",
"device_type": "Android",
"device_id": "",
"profile_image_url": "",
"about": "",
"phone": "123456789",
"last_name": "developer1",
"first_name": "php",
"__v": 0
}
],
"icon_url": "vkVajfRz_400x400.jpg",
"name": "Apt",
"__v": 0
}
]
}
解决方案
你可以试试这个:
db.group.aggregate([{
$lookup:
{
from: "reviews",
let: { roomId: "$data._id" },
pipeline: [
{
$match:
/** Here we're using 'setIntersection' as in lookup comparison of array with array is not possible, only downside is if data :[] in group collection */
{ $expr: { $gt: [{ $size: { $setIntersection: ["$$roomId", "$data.room_id"] } }, 0] } }
}, { $project: { _id: 0, 'data.rating': 1 } }
],
as: "reviews"
}
},
{
$addFields: {
groupJoinStatus: { $cond: [{ $size: '$reviews.data' }, true, false] }, groupRatingStatus: { $cond: [{ $size: '$reviews.data' }, true, false] }
}
}, { $unwind: { path: "$reviews", preserveNullAndEmptyArrays: true } }, {
$addFields: {
avgRating: {
$reduce: {
input: '$reviews.data',
initialValue: '$reviews.data.0.rating',
in: { $avg: ["$$value", "$$this.rating"] }
}
}
}
}, { $project: { reviews: 0 } }
])
测试: MongoDB-游乐场
推荐阅读
- python - Python 相关矩阵 - 只需要绝对值大于 0.5 的列
- python - 错误:需要 Microsoft Visual C++ 14.0。在构建 cython 时使用“Microsoft Visual C++ Build Tools”获取它
- arm-none-eabi-gcc - 无法识别的命令行选项“-arch”;你的意思是'-march ='吗?
- java - 使用 java 流在我的地图函数中发送索引
- python - 使用python OpenCV计算叶脉密度
- postgresql - PostgreSQL 应该在这里使用更多内存吗?
- prestashop - Prestashop 1.7 的自定义 url 路由中的 404 错误
- selenium - 使用 Serenity RestAssured 时出现 noBaseStepListener 错误
- node.js - 当正在运行的容器因任何原因失败时,Kubernetes 是否会重新启动失败的容器或创建新容器?
- node.js - Puppeteer pdf 图像无法正确渲染