Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我如何从mongodb nodejs获得平均评级的产品连接价值?_Node.js_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Node.js 我如何从mongodb nodejs获得平均评级的产品连接价值?

Node.js 我如何从mongodb nodejs获得平均评级的产品连接价值?,node.js,mongodb,mongoose,aggregation-framework,Node.js,Mongodb,Mongoose,Aggregation Framework,参数:- "access_token": "weZVy7pnlV6CtnSQ5w8ZO9iatr5/RZrxc8vc0R5G/kyIiBhfloQuwTIVPIxUupheKHdjhpCjL2g+PsfUPe79cA==", "api_key": "T0BAnEzf6CaWwIMulImtA05nT6fDVjXKHoJM136S", "search_key": "", "latitude": 22.723755, "longitude": 75.886752, "maximum_distance

参数:-

"access_token": "weZVy7pnlV6CtnSQ5w8ZO9iatr5/RZrxc8vc0R5G/kyIiBhfloQuwTIVPIxUupheKHdjhpCjL2g+PsfUPe79cA==",
"api_key": "T0BAnEzf6CaWwIMulImtA05nT6fDVjXKHoJM136S",
"search_key": "",
"latitude": 22.723755,
"longitude": 75.886752,
"maximum_distance": 5000,
"page": 1,
"limit": 5000
我想要的是:-

:----我需要所有组的数据,包括距离(基于搜索的lat long)、平均评分、评分状态、加入状态。

:----按距离同意和平均评分递减排序

:----用户评级状态(如果用户已获得评级,则评级状态为是/否),

:----组加入状态(如果用户已加入该组,则加入状态将为是/否)。

:----按组名搜索。(如果搜索键可用)。

审查和评级表集合结构:
表名=回顾

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 } }
])

测试:

您这样说是什么意思:
如果用户已经获得评分,那么是/否??是否要将
组评级状态添加到
评论
集合的用户评论中??或者,我们是否需要通过检查是否证明一个用户已加入该组以及至少一个用户已给出评论,将这些
组评级状态
组加入状态
字段添加到组文档?当您在
审阅
集合中只有一个匹配文档时,上述查询适用。我想是这样的!!或者您是否在
评论
集合中有多个与给定
数据匹配的文档。\u id
?非常感谢,#非常感谢您对我的帮助,但是我发布了一些缺少的数据库响应和期望,因此在获取正确的数据时遇到了一些问题。@Arpithahu:一旦一个问题得到了预期的答案,请不要完全更改该问题,这会使问题变得笨拙&用户不理解原始帖子的实际意图!!如果您有更改o/p的扩展问题,请提出新问题,但不要完全编辑。。