MongoDB-连接集合,过滤和排序

时间:2019-11-26 09:17:04

标签: mongodb

我有2个收藏集,“业务”和“评论”:

// business row example
{
    "_id" : ObjectId("5ddbc3c1a94f7aac8d179b7c"),
    "business_id" : "vcNAWiLM4dR7D2nwwJ7nCA",
    "full_address" : "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018",
    "hours" : {
        "Tuesday" : {
            "close" : "17:00",
            "open" : "08:00"
        },
        "Friday" : {
            "close" : "17:00",
            "open" : "08:00"
        },
        "Monday" : {
            "close" : "17:00",
            "open" : "08:00"
        },
        "Wednesday" : {
            "close" : "17:00",
            "open" : "08:00"
        },
        "Thursday" : {
            "close" : "17:00",
            "open" : "08:00"
        }
    },
    "open" : true,
    "categories" : [ 
        "Doctors", 
        "Health & Medical"
    ],
    "city" : "Phoenix",
    "review_count" : 7,
    "name" : "Eric Goldberg, MD",
    "neighborhoods" : [],
    "longitude" : -111.983758,
    "state" : "AZ",
    "stars" : 3.5,
    "latitude" : 33.499313,
    "attributes" : {
        "By Appointment Only" : true
    },
    "type" : "business"
}
// review example
{
    "_id" : ObjectId("5ddbc3ea9d4415aa1e6696ae"),
    "votes" : {
        "funny" : 0,
        "useful" : 0,
        "cool" : 0
    },
    "user_id" : "KBLW4wJA_fwoWmMhiHRVOA",
    "review_id" : "dNocEAyUucjT371NNND41Q",
    "stars" : 4,
    "date" : "2012-03-02",
    "text" : "Been going to Dr. Goldberg for over 10 years. I think I was one of his 1st patients when he started at MHMG. He's been great over the years and is really all about the big picture. It is because of him, not my now former gyn Dr. Markoff, that I found out I have fibroids. He explores all options with you and is very patient and understanding. He doesn't judge and asks all the right questions. Very thorough and wants to be kept in the loop on every aspect of your medical health and your life.",
    "type" : "review",
    "business_id" : "vcNAWiLM4dR7D2nwwJ7nCA"
}

我想计算每个企业平均获得多少颗星,并按该值降序排列。到目前为止,我已经:

db.getCollection('review')
.aggregate
([
{
    $group:
    {
        _id: "$business_id",
        avgStars: {$avg: "$stars"}
    }
},
{
    $lookup:
    {
        from: "business",
        localField: "_id",
        foreignField: "business_id",
        as: "business_data"
    }
},
{$unwind: "$business_data"},
{$project: {"name": "$business_data.name", "avgStars": "$avgStars"}},
//{$sort: {avgStars: -1}}
])

没有注释部分,它起作用-向我获取(_id,名称,avgStars)列表。当我取消对$sort函数的注释时,它停止工作-Robo3T无限处理。集合只有50个元素。为什么排序不起作用?

1 个答案:

答案 0 :(得分:0)

以该查询为参考,将$ sort的顺序放在投影阶段之前...

db.review.aggregate([ { 
                         $group: { 
                                   _id:"$business_id", 
                                    avgStars: { $avg:"$stars" } 
                                 } 
                      }, 
                      { 
                         $lookup:{ 
                                   from:"business", 
                                   localField:"_id", 
                                   foreignField:"business_id", 
                                   as:"business_data" 
                                 }  
                     }, 
                     { 
                        $unwind:"$business_data" 
                     },
                     { 
                         $sort :{ "avgStars":-1 }  
                     }, 
                     { 
                         $project:{ 
                              "name":"$business_data.name", 
                              "avgStars":"$avgStars" 
                         } 
                     } 
           ])