Mongodb对获取的$ lookup数据应用条件

时间:2018-08-29 12:37:32

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我从两个不同的集合中获取数据,我在这些数据上应用了$ lookup和$ match,效果很好。我想做的是查找的结果,我再次应用$ match,它具有某些条件。这是我要应用查找的 User 的集合。 用户集合:-

{
"_id": {
    "$oid": "5b714631faaae220d7cc07cf"
},
"name": "abc",
"surname": "xyz",
"email": "abc@gmail.com",
"hotel_data": [
    {
        "location": {
            "type": "Point",
            "coordinates": [
                30.7052881,
                76.84470799999997
            ]
        },
        "_id": {
            "$oid": "5b7fb8559849fd485dc47240"
        },
        "status": true,
       "activityname": "Sparrow",
    },
    {   
        "location": {
            "type": "Point",
            "coordinates": [
                30.733315,
                76.779419
            ]
        },
        "_id": {
            "$oid": "5b7f9ecb9960053dac7ce6f1"
        },
        "status": true,
        "activityname": "Raj Hotel",
       },

] 
}

这是我的可用性集合,我将在其中应用$ lookup和$ match。

{
"_id": {
    "$oid": "5b867766d63c4e2cdd5534d2"
},
"businessid": {
    "$oid": "5b7fb8559849fd485dc47240"
},
"userid": {
    "$oid": "5b714631faaae220d7cc07cf"
},
"hotel_filters": [
    {
        "_id": {
            "$oid": "5b867766d63c4e2cdd5534d3"
        },
        "hotelservice": [
            {
                "service_id": "5b472fff25556c3f02a875aa",
                "service_name": "Pool",
                "_id": {
                    "$oid": "5b867767d63c4e2cdd5534d7"
                }
            },
            {
                "service_id": "5b472fdb25556c3f02a875a9",
                "service_name": "AIR",
                "_id": {
                    "$oid": "5b867767d63c4e2cdd5534d8"
                }
            }
        ],
        "location_type": [
            {
                "locationtype_id": "5b18f4d08c63f42019763b12",
                "locationtype_name": "Scenic View",
                "_id": {
                    "$oid": "5b867767d63c4e2cdd5534d5"
                }
            },
            {
                "locationtype_id": "5b18f4e38c63f42019763b13",
                "locationtype_name": "Central",
                "_id": {
                    "$oid": "5b867767d63c4e2cdd5534d6"
                }
            }
        ],
        "hotel_type": [
            {
                "hoteltype_id": "5b081452edefe23318834a28",
                "hoteltype_name": "3 Star",
                "_id": {
                    "$oid": "5b867767d63c4e2cdd5534d4"
                }
            }
        ]
    }
]
}

我想做的是-在$ lookup之后,我只想要那些将要传递的具有“ hotelservice.service_id”和“ location_type.locationtype_id”的可用性数据。并且我通过对此查询应用尝试了此问题(在此查询中,我仅应用了“ hotelservice.service_id”,但稍后我也想在$和“ location_type.locationtype_id”上应用查询。)

User.aggregate([
        {
            $match:{
                "hotel_data.location": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [30.7052881, 76.84470799999997], 50/ 6371
                        ]
                    }
                }
            }
        },{
            "$unwind": "$hotel_data"
        },
        {
            $match:{
                "hotel_data.location": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [30.7052881, 76.84470799999997], 50/ 6371
                        ]
                    }
                }
            }
        },

        {
            $lookup: {
                from: "availabilities",
                localField: "hotel_data._id",
                foreignField: "businessid",
                as: "availabilitiesdata"
            }
        },
        {$match:{$and: [{"availabilitiesdata.hotel_filters.hotelservice.service_id":{$in:[5b472fb725556c3f02a875a8]}}]}}
    ], function(err, data) {
        if (err) {
            return res.send({data: err, status: false,  msg:"No Hotel Found" });
        }else{ 
            return res.send({status: true,  msg:"Hotel Found", data:data });
        }
    });

此查询给我的结果如下。

{
"status": true,
"msg": "Hotel Found",
"data": [
    {
        "_id": "5b714631faaae220d7cc07cf",
        "name": "abc",
        "surname": "xyz",
        "email": "abc@gmail.com",
        "hotel_data": {
            "location": {
                "type": "Point",
                "coordinates": [
                    30.7052881,
                    76.84470799999997
                ]
            },
            "_id": "5b7fb8559849fd485dc47240",
            "status": true,
            "activityname": "Sparrow",

        },
        "availabilitiesdata": [
            {
                "_id": "5b864fe68ab0b71f4f28021e",
               "businessid": "5b7fb8559849fd485dc47240",
                "userid": "5b714631faaae220d7cc07cf",
                "hotel_filters": [
                    {
                        "_id": "5b864fe78ab0b71f4f28021f",
                        "hotelservice": [
                            {
                                "service_id": "5b472fb725556c3f02a875a8",
                                "service_name": "Parking",
                                "_id": "5b864fe78ab0b71f4f280223"
                            },
                            {
                                "service_id": "5b472fff25556c3f02a875aa",
                                "service_name": "Pool",
                                "_id": "5b864fe78ab0b71f4f280224"
                            }
                        ],
                        "location_type": [
                            {
                                "locationtype_id": "5b18f4798c63f42019763b11",
                                "locationtype_name": "Quiet",
                                "_id": "5b864fe78ab0b71f4f280221"
                            },
                            {
                                "locationtype_id": "5b18f4e38c63f42019763b13",
                                "locationtype_name": "Central",
                                "_id": "5b864fe78ab0b71f4f280222"
                            }
                        ],
                        "hotel_type": [
                            {
                                "hoteltype_id": "5b0813e2edefe23318834a27",
                                "hoteltype_name": "5 Star",
                                "_id": "5b864fe78ab0b71f4f280220"
                            }
                        ]
                    }
                ]
            },
            {
                "_id": "5b867766d63c4e2cdd5534d2",
                "businessid": "5b7fb8559849fd485dc47240",
                "userid": "5b714631faaae220d7cc07cf",
                "hotel_filters": [
                    {
                        "_id": "5b867766d63c4e2cdd5534d3",
                        "hotelservice": [
                            {
                                "service_id": "5b472fff25556c3f02a875aa",
                                "service_name": "Pool",
                                "_id": "5b867767d63c4e2cdd5534d7"
                            },
                            {
                                "service_id": "5b472fdb25556c3f02a875a9",
                                "service_name": "AIR",
                                "_id": "5b867767d63c4e2cdd5534d8"
                            }
                        ],
                        "location_type": [
                            {
                                "locationtype_id": "5b18f4d08c63f42019763b12",
                                "locationtype_name": "Scenic View",
                                "_id": "5b867767d63c4e2cdd5534d5"
                            },
                            {
                                "locationtype_id": "5b18f4e38c63f42019763b13",
                                "locationtype_name": "Central",
                                "_id": "5b867767d63c4e2cdd5534d6"
                            }
                        ],
                        "hotel_type": [
                            {
                                "hoteltype_id": "5b081452edefe23318834a28",
                                "hoteltype_name": "3 Star",
                                "_id": "5b867767d63c4e2cdd5534d4"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]}

但是我想要的就是这样。

{
"status": true,
"msg": "Hotel Found",
"data": [
    {
        "_id": "5b714631faaae220d7cc07cf",
        "name": "abc",
        "surname": "xyz",
        "email": "abc@gmail.com",
        "hotel_data": {
            "location": {
                "type": "Point",
                "coordinates": [
                    30.7052881,
                    76.84470799999997
                ]
            },
            "_id": "5b7fb8559849fd485dc47240",
            "status": true,
            "activityname": "Sparrow",

        },
        "availabilitiesdata": [
            {
                "_id": "5b864fe68ab0b71f4f28021e",
               "businessid": "5b7fb8559849fd485dc47240",
                "userid": "5b714631faaae220d7cc07cf",
                "hotel_filters": [
                    {
                        "_id": "5b864fe78ab0b71f4f28021f",
                        "hotelservice": [
                            {
                                "service_id": "5b472fb725556c3f02a875a8",
                                "service_name": "Parking",
                                "_id": "5b864fe78ab0b71f4f280223"
                            },
                            {
                                "service_id": "5b472fff25556c3f02a875aa",
                                "service_name": "Pool",
                                "_id": "5b864fe78ab0b71f4f280224"
                            }
                        ],
                        "location_type": [
                            {
                                "locationtype_id": "5b18f4798c63f42019763b11",
                                "locationtype_name": "Quiet",
                                "_id": "5b864fe78ab0b71f4f280221"
                            },
                            {
                                "locationtype_id": "5b18f4e38c63f42019763b13",
                                "locationtype_name": "Central",
                                "_id": "5b864fe78ab0b71f4f280222"
                            }
                        ],
                        "hotel_type": [
                            {
                                "hoteltype_id": "5b0813e2edefe23318834a27",
                                "hoteltype_name": "5 Star",
                                "_id": "5b864fe78ab0b71f4f280220"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]}  

我只希望那些与$ lookup可用性数据上的条件匹配的可用性。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用$lookup管道变体将$match应用于3.6中的联接集合中

类似于(用下面的查找管道替换$lookup$match阶段)

{"$lookup":{
  "from":"availabilities",
  "let":{"hotel_data_id":"$hotel_data._id"},
  "pipeline":[
    {"$match":{
      "hotel_filters.hotelservice.service_id":{"$in":["5b472fb725556c3f02a875a8"]},
      "hotel_filters.location_type.locationtype_id":{"$in":["5b18f4798c63f42019763b11"]},
      "$expr":{"$eq":["$$hotel_data_id","$businessid"]}
    }}
  ],
  "as":"availabilitiesdata"
}}