MongoDb extract array

Viewed 72

I have a mongodb which I want to extract some specific data. Here is my json:

 {
    "jobs" : [
        {
            "id": 554523,
            "code": "1256-554523",
            "name": "Banco de Talentos",
            "status": "published",
            "type": "vacancy_type_effective",
            "publicationType": "external",
            "numVacancies": 1,
            "departmentId": 108141,
            "departmentName": "FUTURAS OPORTUNIDADES",
            "roleId": 169970,
            "roleName": "BANCO DE TALENTOS",
            "createdAt": "2020-10-30T12:23:48.572Z",
            "updatedAt": "2020-12-30T23:21:30.403Z",
            "branchId": null,
            "branchName": null
        },
        {
            "id": 616834,
            "code": "1256-616834",
            "name": "YYYYYY (o)  YYYYY",
            "status": "frozen",
            "type": "vacancy_type_effective",
            "publicationType": "external",
            "numVacancies": 1,
            "departmentId": 109190,
            "departmentName": "TESTE TESTE",
            "roleId": 165712,
            "roleName": "SL - TESTE PL",
            "createdAt": "2020-12-16T14:17:36.187Z",
            "updatedAt": "2021-01-29T17:08:43.613Z",
            "branchId": 120448,
            "branchName": "TESTE TESTE1"
        }
    ],
    "application": [
        {
            "id": 50707344,
            "score": 40.251965332031254,
            "partnerName": "indeed",
            "endedAt": null,
            "createdAt": "2020-12-21T11:21:30.587Z",
            "updatedAt": "2021-02-18T22:02:35.866Z",
            "tags": {},
            "candidate": {
                "birthdate": "1986-04-04",
                "id": 578615,
                "name": "TESTE",
                "lastName": "TESTE TESTE",
                "email": "teste@teste.com.br",
                "identificationDocument": "34356792807",
                "countryOfOrigin": "BR",
                "linkedinProfileUrl": "teste",
                "gender": "female",
                "mobileNumber": "+5511972319799",
                "phoneNumber": "(11)2463-2039"
            },
            "job": {
                "id": 619713,
                "name": "XXXXde XXXX Pleno"
            },
            "manualCandidate": null,
            "currentStep": {
                "id": 3527370,
                "name": "Cadastro",
                "status": "done"
            }
        },
        {
            "id": 50707915,
            "score": 3.75547943115234E+1,
            "partnerName": "indeed",
            "endedAt": null,
            "createdAt": "2020-12-21T11:31:31.877Z",
            "updatedAt": "2021-02-18T14:07:06.605Z",
            "tags": {},
            "candidate": {
                "birthdate": "1971-10-02",
                "id": 919358,
                "name": "TESTE TESTE",
                "lastName": "SILVA",
                "email": "teste.teste@teste.com",
                "identificationDocument": "3232323232",
                "countryOfOrigin": "BR",
                "linkedinProfileUrl": "teste/",
                "gender": "female",
                "mobileNumber": "11 94021- 5521",
                "phoneNumber": "+5511995685247"
            },
            "job": {
                "id": 619713,
                "name": "Analista de XXXXX Pleno"
            },
            "manualCandidate": null,
            "currentStep": {
                "id": 3527370,
                "name": "Cadastro",
                "status": "done"
            }
        }
    ]
}

My question is: How can I extract only the array objects in jobs and application separately? Anyone knows the code in Mongodb for do this? I need do this task for after I can insert the extract separated data in different collections.

Thanks a lot.

1 Answers

Unfortunately there is no real "good" way of doing this. Here is an example of how I would do it by using $facet and other operators to manipulate the structure

db.collection.aggregate([
  {
    $match: {
      /** your query*/
      
    }
  },
  {
    $facet: {
      jobs: [
        {
          $unwind: "$jobs"
        },
        {
          $replaceRoot: {
            newRoot: "$jobs"
          }
        }
      ],
      applications: [
        {
          $unwind: "$application"
        },
        {
          $replaceRoot: {
            newRoot: "$application"
          }
        }
      ]
    }
  },
  {
    $addFields: {
      "merged": {
        "$concatArrays": [
          "$jobs",
          "$applications"
        ]
      }
    }
  },
  {
    $unwind: "$merged"
  },
  {
    "$replaceRoot": {
      "newRoot": "$merged"
    }
  }
])

Mongo Playground

I would personally just do it in code after you fetched the document.

Related