Несколько операций агрегации с использованием MongoDB в одной операции - PullRequest
1 голос
/ 09 января 2020

У меня есть 2 коллекции adminUser и DepartmentUser , которые связаны полем adminUserId из adminUser collection и adminUserId из DepartmentUser collection.

Я хочу объединить эти две коллекции, которые содержат все поля обеих коллекций, независимо от того, являются ли данные общими или редкими между ними. Я попытался с помощью агрегации, но агрегация возвращает данные только с общими полями.

adminUser :

    {
        "adminUserId" : "1"
        "userName" : "Smith",
        "position" : "Head"
    },
    {
        "adminUserId" : "2"
        "userName" : "Joe",
        "position" : "Lead"
    },
    {
        "adminUserId" : "3"
        "userName" : "Mark",
        "position" : "Lead"
    }

DepartmentUser :

    {
        "userId" : "1"
        "userName" : "Leslie",
        "position" : "Head",
        "adminUserId" : ""
    },
    {
        "userId" : "2"
        "userName" : "Joe",
        "position" : "Lead",
        "adminUserId" : "2"
    },
    {
        "userId" : "3"
        "userName" : "Mark",
        "position" : "Lead",
        "adminUserId" : "3"
    },
    {
        "userId" : "4"
        "userName" : "Allen",
        "position" : "Lead",
        "adminUserId" : ""
    }

Вывод :

    {
        "adminUserId" : "1"
        "userName" : "Smith",
        "position" : "Head"
    },
    {
        "adminUserId" : "2"
        "userName" : "Joe",
        "position" : "Lead",
        "departmentUserinfo":{
               "userId" : "2"
               "userName" : "Joe",
               "position" : "Lead",
               "adminUserId" : "2"
        }
    },
    {
        "adminUserId" : "3"
        "userName" : "Mark",
        "position" : "Lead",
        "departmentUserinfo":{
               "userId" : "2"
               "userName" : "Mark",
               "position" : "Lead",
               "adminUserId" : "3"
        }
    },
    {
        "adminUserId" : ""
        "userName" : "",
        "position" : "",
        "departmentUserinfo":{
               "userId" : "1"
               "userName" : "Leslie",
               "position" : "Head",
               "adminUserId" : ""
        }
    },
    {
        "adminUserId" : ""
        "userName" : "",
        "position" : "",
        "departmentUserinfo":{
               "userId" : "4"
               "userName" : "Allen",
               "position" : "Lead",
               "adminUserId" : ""
        }
    }

Может кто-нибудь помочь?

1 Ответ

1 голос
/ 09 января 2020

Попробуйте:

db.adminUser.aggregate([
    {
        $lookup:
        {
            from: "departmentUser",
            localField: "adminUserId",
            foreignField: "adminUserId",
            as: "departmentUserinfo"
        }
    }, 
    // As $lookup will result in an array i.e; departmentUserinfo will be an array, So getting first element out of it as we know it will always be [{}] --> If match found or [] --> If no match
    { $addFields: { departmentUserinfo: { $arrayElemAt: ['$departmentUserinfo', 0] } } }
])

Результат:

/* 1 */
{
    "_id" : ObjectId("5e1757b919c2d113022f4584"),
    "adminUserId" : "1",
    "userName" : "Smith",
    "position" : "Head"
}

/* 2 */
{
    "_id" : ObjectId("5e1757b919c2d113022f4585"),
    "adminUserId" : "2",
    "userName" : "Joe",
    "position" : "Lead",
    "departmentUserinfo" : {
        "_id" : ObjectId("5e1757f219c2d113022f4588"),
        "userId" : "2",
        "userName" : "Joe",
        "position" : "Lead",
        "adminUserId" : "2"
    }
}

/* 3 */
{
    "_id" : ObjectId("5e1757b919c2d113022f4586"),
    "adminUserId" : "3",
    "userName" : "Mark",
    "position" : "Lead",
    "departmentUserinfo" : {
        "_id" : ObjectId("5e1757f219c2d113022f4589"),
        "userId" : "3",
        "userName" : "Mark",
        "position" : "Lead",
        "adminUserId" : "3"
    }
}
...