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.

3

1 Answer 1

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
  }
])
Sign up to request clarification or add additional context in comments.

9 Comments

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 }, ] }
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.
Yes... That's exactly what I needed! Thank you so much (AGAIN)!! =)
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..???? mongoplayground.net/p/W4hBM0KN1UD mongoplayground.net/p/W4hBM0KN1UD
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.