簡體   English   中英

如何通過 mongodb nodejs 的平均評分連接產品獲得價值?

[英]How can I get value with join of product with average rating from 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 將為 yes/no )。

:---- 按組名搜索。 (如果搜索鍵可用)。

評論和評分表 收藏結構:
表名 = 評論。

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-Playground

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM