3

I am new to the mongoDB aggregation and I have this situation. I have this Json and I need to convert by "select" this object:

{
    "type": "PF",
    "code": 12345
    "Name": Darth Vader,
    "currency": "BRL",
    "status": "SINGLE",
    "adress": [
        {
            "localization": "DEATH STAR",
            "createDate": 1627990848665
        },
        {
            "localization": "TATOOINE",
            "createDate": 1627990555665
        },
    ]
}

this way:

{
    "type": "PF",
    "code": 12345
    "Name": Darth Vader,
    "currency": "BRL",
    "status": "SINGLE",
    "localization": "DEATH STAR",
    "createDate": 1627990848665
},
{
    "type": "PF",
    "code": 12345
    "Name": Darth Vader,
    "currency": "BRL",
    "status": "SINGLE",
    "localization": "TATOOINE",
    "createDate": 1627990555665
}

So, after my query is complete, I will have 02 objects instead of 01. How can I do this? I would like to do this via select because after converting I will sort by createDate and limit the number of records to return to the API. I'm using Criteria em my project.

  • Does this answer your question? [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – ℛɑƒæĿᴿᴹᴿ Aug 31 '21 at 20:17
  • Unfortunately no because I need to repeat the same informations of the first element to others. =( – Patrick Teixeira Aug 31 '21 at 20:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 03 '21 at 14:15

1 Answers1

2

The way to do this is $unwind, this will make 1 copy of the document, for each member of the array.

Test code here

db.collection.aggregate([
  {
    "$unwind": {
      "path": "$user.adress"
    }
  },
  {
    "$set": {
      "user": {
        "$mergeObjects": [
          "$user",
          "$user.adress"
        ]
      }
    }
  },
  {
    "$unset": [
      "user.adress"
    ]
  },
  {
    "$sort": {
      "createDate": 1
    }
  },
  {
    "$limit": 10
  }
])

Edit1 (the above is if user is a field, if it was the name of the collection)

  • $$ROOT is a system variable that has as value all the document

Test code here

Query

db.collection.aggregate([
  {
    "$unwind": {
      "path": "$adress"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$mergeObjects": [
          "$$ROOT",
          "$adress"
        ]
      }
    }
  },
  {
    "$unset": [
      "adress"
    ]
  },
  {
    "$sort": {
      "createDate": 1
    }
  },
  {
    "$limit": 10
  }
])
Takis
  • 8,314
  • 2
  • 14
  • 25
  • I have just a only question. I'm looking in my json and I made a mistake... I dont have the name USER. How can I do in this situation? Only this way... { "type": "PF", "code": 12345 "Name": Darth Vader, "currency": "BRL", "status": "SINGLE", "adress": [ { "localization": "DEATH STAR", "createDate": 1627990848665 }, { "localization": "TATOOINE", "createDate": 1627990555665 }, ] } – Patrick Teixeira Sep 01 '21 at 13:34
  • i was wondering a bit, if user was a field or the name of the collection, try the edit, i think you will be fine. – Takis Sep 01 '21 at 13:59
  • Yes... That's exactly what I needed! Thank you so much (AGAIN)!! =) – Patrick Teixeira Sep 01 '21 at 14:03
  • I remember about another situation... I have the actual location in the Json and I need to keep the actual location and reply the other informations for the "children" as you show me. How I keep the actual location in this case and add the "children" for the return..???? https://mongoplayground.net/p/W4hBM0KN1UD https://mongoplayground.net/p/W4hBM0KN1UD – Patrick Teixeira Sep 08 '21 at 20:09
  • 1
    i am confused a bit, if its different question maybe ask new one, with new data and the expected output you want. so people can help. – Takis Sep 08 '21 at 20:15
  • I undestend... so... I create a new questuions... https://stackoverflow.com/questions/69117709/reply-head-informations-and-add-extract-array-to-same-result If you have time and could please help me again... I'll be very thankful – Patrick Teixeira Sep 09 '21 at 11:56
  • Takis, this situation caused this... https://stackoverflow.com/questions/69209289/mongodb-unset-if-condition-is-met ....could you help me? – Patrick Teixeira Sep 16 '21 at 13:45
  • Hi Takis... =) Could you please help me again? https://stackoverflow.com/questions/69916346/mongodb-updatemany – Patrick Teixeira Nov 10 '21 at 15:54
  • how you doing? =) Could you please help me again? https://stackoverflow.com/questions/74944874/mongodb-get-last-transaction-per-ucode – Patrick Teixeira Dec 28 '22 at 20:54