对象数组

时间:2016-07-26 04:47:59

标签: mongodb mongoose mongoose-populate

我有2个收藏结构如下。我已经尝试了$ lookup来获得结果,但由于我的本地和外部字段都在对象数组中,因此我没有得到任何结果。

以下是我的结构:

{
    "_id" : ObjectId("5795a3531d3f3afc19caefef"),
    "name" : "category1",
    "updatedAt" : "1469431592786",
    "resources" : [ 
        {
            "_id" : ObjectId("5791be003fa3bedc15d3adde"),
            "title" : "resource1",
            "availability" : false
        }, 
        {
            "_id" : ObjectId("5795a3771d3f3afc19caeff0"),
            "title" : "resource2",
            "availability" : true
        }
    ]

}

上面的“类别”架构有对象的资源数组。此资源_id以下列方式存储在预订集合中:

"booking":
{
    "_id" : ObjectId("57960aa8000ca7a46b7ef683"),
    "name" : "1469491200000",
    "__v" : 0,
    "schedule" : [ 
        {
            "resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
            "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
            "title" : "grofep",
            "_id" : ObjectId("57960aa8f9f9951c1fc923b1")
        },
        {
            "resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
            "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
            "title" : "mr3",
            "_id" : ObjectId("57960aa8f9f9951c1fc923b2")
        },
        {
            "resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
            "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
            "title" : "grofep23",
            "_id" : ObjectId("57960aa8f9f9951c1fc923b3")
        }
    ]
}

现在我想获得所有预订收集的时间表及其资源信息。我想根据预订时间表从类别表中获取资源。 期望的输出:

[
    {
        "name" : "1469491200000",
        "resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
        "resourceTitle":"title",
        "availability":false,
        "bookings": [
            {
                "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
                "title" : "grofep",
                "_id" : ObjectId("57960aa8f9f9951c1fc923b1")
            },
            {
                "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
                "title" : "grofep23",
                "_id" : ObjectId("57960aa8f9f9951c1fc923b3")
            }
        ]
    },
    {
        "name" : "1469491200000",
        "resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
        "resourceTitle":"mr3",
        "availability":false,
        "bookings": [
            {
                "userId" : ObjectId("5791be003fa3bedc15d3adcve"),
                "title" : "mr3",
                "_id" : ObjectId("57960aa8f9f9951c1fc923b2")
            }
        ]
    }
]

帮助我获得所需的结果。 感谢。

0 个答案:

没有答案