0

I have data in the form of array of objects, Objects contain review and its key value pair and it has reviewDetail . Following is the data

[
    {
        "review": {
            "reviewDetail": {
                "title": "review 2",
                "description": "this is description",
                "file": [
                    "1659414826665-cropped-cropped-logo-e1620197043895-2.png.png"
                ],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62e8a92a0c6e7159a7b934d8",
                "createdAt": "2022-08-02T04:33:46.677Z",
                "updatedAt": "2022-08-02T04:37:28.213Z"
            }
        }
    },
    {
        "review": {
            "reviewDetail": {
                "title": "review 1",
                "description": "this is description of review",
                "file": [
                    "1659414880490-cropped-cropped-logo-e1620197043895-2.png.png"
                ],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62e8a9600c6e7159a7b934df",
                "createdAt": "2022-08-02T04:34:40.499Z",
                "updatedAt": "2022-08-06T12:44:04.633Z"
            }
        }
    },
    {
        "review": {
            "reviewDetail": {
                "title": "review 1",
                "description": "this is description of review",
                "file": [],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62ee5e76e9934990d6911e21",
                "createdAt": "2022-08-06T12:28:38.457Z",
                "updatedAt": "2022-08-06T13:24:03.074Z"
            }
        }
    }
]

i want to extract reviewDetails in the array : following is the desired result

[
              {
                "title": "review 2",
                "description": "this is description",
                "file": [
                    "1659414826665-cropped-cropped-logo-e1620197043895-2.png.png"
                ],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62e8a92a0c6e7159a7b934d8",
                "createdAt": "2022-08-02T04:33:46.677Z",
                "updatedAt": "2022-08-02T04:37:28.213Z"
            },
            {
                "title": "review 1",
                "description": "this is description of review",
                "file": [
                    "1659414880490-cropped-cropped-logo-e1620197043895-2.png.png"
                ],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62e8a9600c6e7159a7b934df",
                "createdAt": "2022-08-02T04:34:40.499Z",
                "updatedAt": "2022-08-06T12:44:04.633Z"
            },
             {
                "title": "review 1",
                "description": "this is description of review",
                "file": [],
                "viewed": 0,
                "isDeleted": true,
                "_id": "62ee5e76e9934990d6911e21",
                "createdAt": "2022-08-06T12:28:38.457Z",
                "updatedAt": "2022-08-06T13:24:03.074Z"
            }
]

I tried to extract in particular pattern using Javascript in node js but needed using aggregation pipeline

1
  • If these are the your documents use project for this Commented Sep 4, 2022 at 10:07

1 Answer 1

1

You can simply use this to achieve your desired output

collectionName.aggregate([
   {
     $replaceRoot:{ newRoot:"$review.reviewDetail" }
   }    
])

Sign up to request clarification or add additional context in comments.

Comments

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.