3

I have an array of object as given with unknown number of array elements:

               { "content": [
                    {
                        "_id": "refbooks",
                        "total": 189,
                        "published": 189,
                        "created": 0,
                        "approved": 0,
                        "rejected": 0,
                        "sent_for_approval": 0
                    },
                    {
                        "_id": "weblinks",
                        "total": 1911,
                        "published": 1899,
                        "created": 10,
                        "approved": 2,
                        "rejected": 0,
                        "sent_for_approval": 0
                    },.................]}

I want to convert it to an object like given:

                    { "content": {
                    {
                        "_id": "refbooks",
                        "total": 189,
                        "published": 189,
                        "created": 0,
                        "approved": 0,
                        "rejected": 0,
                        "sent_for_approval": 0
                    },
                    {
                        "_id": "weblinks",
                        "total": 1911,
                        "published": 1899,
                        "created": 10,
                        "approved": 2,
                        "rejected": 0,
                        "sent_for_approval": 0
                    },.................}}

I tried using $unwind, but I did't get my required output. How to implement this inside aggregation pipeline in MongoDB?

I'm using Mongo version 3.4.

2
  • That's an impossible setup... Not sure what you mean. Whenever you have a collection of things it has to be wrapped in an array...? Commented Oct 25, 2018 at 11:34
  • your expected result is not a valid object. Commented Oct 25, 2018 at 11:48

2 Answers 2

18

I found the solution.

{
            $project: {
                "content": {
                    "$arrayToObject": {
                        "$map": {
                            "input": "$content",
                            "as": "el",
                            "in": {
                                "k": "$$el._id",
                                "v": "$$el"
                            }
                        }
                    }
                }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I guess the second one is not a proper object format you need a key value pare for make object

{ "content": {
                   "key1":{
                        "_id": "refbooks",
                        "total": 189,
                        "published": 189,
                        "created": 0,
                        "approved": 0,
                        "rejected": 0,
                        "sent_for_approval": 0
                    },
                   "key2":{
                        "_id": "weblinks",
                        "total": 1911,
                        "published": 1899,
                        "created": 10,
                        "approved": 2,
                        "rejected": 0,
                        "sent_for_approval": 0
                    }}}

2 Comments

Yeah, like that
@LabeebShareef i guess you already find the answer there is another way for doing same by using underscore.js underscorejs.org/#groupBy

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.