0

I have a collection that replays with the data below when i get all documents. But i would like to add a custom "grouping" to it so it looks like example 2 below. And i though you could do it by using Population but im not getting it to work..

Original json

[
      {
      "id" : "...",
      "category" : "...",
      "header" : "...",
      "body" : "..."
   },
      {
      "id" : "...",
      "category" : "...",
      "header" : "...",
      "body" : "..."
   }
]

What i want:

{
  "offers" : [ {
    "id" : "...",
    "category" : "...",
    "header" : "...",
    "body" : "..."
     },
     {
    "id" : "...",
    "category" : "...",
    "header" : "...",
    "body" : "..."
     }]
}

2 Answers 2

1

Well, you can just create that object yourself:

var obj = { offers : results };

(where results is your Mongoose result set).

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

1 Comment

Aah that was maybe the best and easiest way. Thanks!
0

By using the lodash module in npm, we can select the required felid in JSON respone

   import mongoose from 'mongoose'
   import pick from 'lodash/pick'



    UserSchema.methods.toJSON = function () 
       let user = this
       let userObject = user.toObject()
       return pick(userObject, ['_id', 'email'])
       }

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.