猫鼬聚合与鉴别

时间:2018-07-27 08:05:25

标签: node.js mongodb mongoose aggregation-framework azure-cosmosdb

我仅将CosmosDB Mongo API与一个使用区分符的集合一起使用。 我现在遇到一个问题,想将其他集合“合并”成一个集合。 我的常用集合名为“ alldata”,并具有区分符键(_type),“会话”,“房间”和“用户”。

会话包含“ roomId”和“ speakerIds”(用户ID数组)。

会话对象:

{
  "singleEntrance": false,
  "accreditationRequired": false,
  "closed": false,
  "usersAtended": [],
  "speakerIds": [
    "5b573723930d9751768f266e",
    "5b586b6032063a70632721d8"
  ],
  "archived": false,
  "_type": "Session",
  "_id": "5b586f81ae744a7266524f84",
  "title": "Bla bla",
  "about": "",
  "type": "SIS1",
  "track": "Special Interest Session",
  "venue": "Bla 1",
  "roomId": "5b5731e764f0de4f9e4082ac",
  "from": "2018-10-22T10:00:00.015Z",
  "to": "2018-10-22T12:00:00.015Z"
}

我想要的结果:

{
  "singleEntrance": false,
  "accreditationRequired": false,
  "closed": false,
  "usersAtended": [],
  "spikeri": [
    {* LIST OF USER OBJECTS *}
  ],
  "archived": false,
  "_type": "Session",
  "_id": "5b586f81ae744a7266524f84",
  "title": "Bla bla",
  "about": "",
  "type": "SIS1",
  "track": "Special Interest Session",
  "venue": "Bla 1",
  "room": {
            "_id": "5b5731e764f0de4f9e4082ac",
            "archived": false,
            "_type": "Room",
            "name": "Bla 1",
            "location": "Hotel Bla",
            "createdAt": "2018-07-24T14:04:23.370Z",
            "updatedAt": "2018-07-24T14:04:23.370Z",
            "__v": 0
        },
  "from": "2018-10-22T10:00:00.015Z",
  "to": "2018-10-22T12:00:00.015Z"
}

房间工作正常,但是我无法使用户(“扬声器”)正常工作。 最后,下面是代码:

async getAgenda() {
    return await Session.aggregate([
        {
            // this query returns all Sessions...
            $match: {
                $and: [
                    { from: { $gte: new Date("2018-10-22T10:00:00.015Z") } },
                    { to: { $lte: new Date("2018-10-22 17:00:00.015Z") } },
                ]
            }
        },
        {
            $lookup: { from: "alldata", localField: "roomId", foreignField: "_id", as: "room" }
        },
        {
            $unwind: { path: '$room' }
        },
        {
            $lookup: { from: "alldata", localField: "speakerIds", foreignField: "_id", as: "spikeri" }
        }
    ])
}

更新:用户对象(说话者==用户):

{
        "_id": "5b2ba7784ad0f226d8ae2788",
        "group": "USER",
        "verified": true,
        "eventsRegistered": [
            "5b2b77b74ad0f226d8ae2780",
            "5b27ca01cc7eff056826acc7"
        ],
        "eventsAttended": [],
        "tickets": [
            "5b27cc43cc7eff056826accb",
            "5b27d284cc7eff056826acd1",
            "5b27cceecc7eff056826acd0"
        ],
        "contacts": [],
        "meetings": [],
        "_type": "User",
        "email": "bla@blabla.us",
        "password": "...",
        "countryCode": "HR",
        "firstName": "Blaman",
        "lastName": "Bla bla",
        "role": "SPEAKER",
        "createdAt": "2018-06-21T13:26:16.686Z",
        "updatedAt": "2018-07-25T14:21:42.055Z",
        "__v": 0
        "confirmationCode": "5375159",
        "transactionIds": [
            "..."
        ],
        "transactionId": "...",
        "gender": "",
        "jobTitle": "",
        "companyAddress": "",
        "companyName": "",
        "postalCode": "",
        "city": "",
        "country": "Croatia"
    }

谢谢您的帮助!

0 个答案:

没有答案