JSON格式的MongoDB查询问题未返回任何结果

时间:2019-02-07 16:22:39

标签: arrays json mongodb nosql aggregate-functions

{
    "collection": "capacities",
    "aggregate": [
        {
            "$lookup": {
                "from": "photographers",
                "localField": "photographerId",
                "foreignField": "_id",
                "as": "photographer"
            }
        },
        {
            "$unwind": {
                "path": "$photographer"
            }
        },
        {
            "$lookup": {
                "from": "users",
                "localField": "photographer.user",
                "foreignField": "_id",
                "as": "userDocument"
            }
        },
        {
            "$unwind": {
                "path": "$userDocument"
            }
        },
        {
            "$match": {
                "status": "confirmed",
                "start": {
                    "$gte": "ISODate(\"{{ start }}\")"
                },
                "end": {
                    "$lte": "ISODate(\"{{ end }}\")"
                },
                "photographer.types": "{{ type }}"
            }
        },
        {
            "$addFields": {
                "longitude": {
                    "$arrayElemAt": [
                        "$location.geometry.coordinates",
                        0
                    ]
                },
                "latitude": {
                    "$arrayElemAt": [
                        "$location.geometry.coordinates",
                        1
                    ]
                }
            }
        },
        {
            "$group": {
                "_id": {
                    "name": "$userDocument.name",
                    "city": "$city",
                    "longitude": "$longitude",
                    "latitude": "$latitude",
                    "photographerId": "$photographerId"
                },
                "capacityHours": {
                    "$sum": {
                        "$divide": [
                            {
                                "$subtract": [
                                    "$end",
                                    "$start"
                                ]
                            },
                            3600000
                        ]
                    }
                }
            }
        },
        {
            "$lookup": {
                "from": "shoots",
                "localField": "photographerId",
                "foreignField": "photographerId",
                "as": "photographershoots"
            }
        },
        {
            "$unwind": {
                "path": "$photographershoots"
            }
        },
        {
            "$match": {
                "photographershoots.status": "confirmed",
                "photographershoots.start": {
                    "$gte": "ISODate(\"{{ start }}\")"
                },
                "photographershoots.end": {
                    "$lte": "ISODate(\"{{ end }}\")"
                }
            }
        },
        {
            "$group": {
                "_id": {
                    "name": "$userDocument.name",
                    "capacityHours": "$capacityHours",
                    "city": "$city",
                    "longitude": "$longitude",
                    "latitude": "$latitude"
                },
                "shootHours": {
                    "$sum": {
                        "$divide": [
                            {
                                "$subtract": [
                                    "$photographershoots.end",
                                    "$photographershoots.start"
                                ]
                            },
                            3600000
                        ]
                    }
                }
            }
        },
        {
            "$addFields": {
                "hours": {
                    "$subtract": [
                        "$capacityHours",
                        "$shootHours"
                    ]
                }
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您是否可以尝试将此查询分成更小的部分并逐一进行故障排除?

谢谢