1

I need to group the data with set of fields using mongodb aggregate. Here is my data in the collection

[
    {
        bookName: "aaaa",
        bookNo: "1",
        registeredDate: "2018-02-01T06:51:16.738Z"
    },
    {
        bookName: "bbbb",
        bookNo: "2",
        registeredDate: "2018-02-01T06:51:29.244Z"
    }
    {
        bookName: "cccc",
        bookNo: "1",
        registeredDate: "2018-02-01T06:51:29.244Z"
    }
    {
        bookName: "dddd",
        bookNo: "2",
        registeredDate: "2018-02-01T06:51:29.244Z"
    }
]

I need to aggregate the above data into group by and show the data inside the group. Here is the output that i need,

{
    Books: [
        {
        bookNo: "1",
            books: [{
                    bookName: "aaaa",
                    registeredDate: "2018-02-01T06:51:16.738Z"
                },
                {
                    bookName: "cccc",
                    registeredDate: ""2018-02-01T06:51:29.244Z"
                }]
        },
        {
        bookNo: "2",
            books: [{
                bookName: "bbbb",
                registeredDate: "2018-02-01T06:51:29.244Z"
            },
            {
                bookName: "dddd",
                registeredDate: "2018-02-01T06:51:29.244Z"
            }]
        }
    ]
}

Someone help me find a solution.

Thanks in Advance,

skr07
  • 707
  • 1
  • 10
  • 36

2 Answers2

3

Try following aggregation:

db.books.aggregate([
    {
        $group: {
            _id: "$bookNo",
            books: {
                $push: {
                    bookName: "$bookName",
                    registeredDate: "$registeredDate"
                }
            }
        }
    },
    {
        $project:{
            _id: 0,
            bookNo: "$_id",
            books: 1
        }
    }
])

In $group you can specify which fields will be $pushed from each document. Then you can use $project to just rename _id to bookNo field

mickl
  • 48,568
  • 9
  • 60
  • 89
  • Thanks mickl , but the order is different, it shows like `books:[{..}], bookNo:2`, I need it in reverse order as `bookNo:2, books:[{..}]`. is anything need to change. – skr07 Feb 01 '18 at 07:44
  • Hey, to be honest you shouldn't rely on the order of keys in any JSON-like structure: https://stackoverflow.com/questions/5046835/mongodb-field-order-and-document-position-change-after-update/6453755#6453755 – mickl Feb 01 '18 at 08:18
0
db.collection.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $group: {
                _id: '$bookNo',
                //Books:{$addToSet:{bookNo:'$bookNo'}},
                books: {
                    $addToSet: {
                        bookname: '$bookName',
                        registeredDate: '$registeredDate'
                    }
                },

            }
        },

        // Stage 2
        {
            $group: {
                _id: null,
                Books: {
                    $addToSet: {
                        books: '$books',
                        bookNo: '$_id'
                    }
                }
            }
        },

        // Stage 3
        {
            $project: {
                _id: 0,
                Books: 1
            }
        }

    ]


);
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26