Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning whole object in MongoDB aggregation

I have Item schema in which I have item details with respective restaurant. I have to find all items of particular restaurant and group by them with 'type' and 'category' (type and category are fields in Item schema), I am able to group items as I want but I wont be able to get complete item object. My query:

db.items.aggregate([{
    '$match': {
        'restaurant': ObjectId("551111450712235c81620a57")
    }
}, {
    '$group': {
        id: {
            '$push': '$_id'
        }
        , _id: {
            type: '$type'
            , category: '$category'
        }
    }
}, {
    $project: {
        id: '$id'
    }
}])

I have seen one method by adding each field value to group then project it. As I have many fields in my Item schema I don't feel this will good solution for me, Can I get complete object instead of Ids only.

like image 231
zulekha Avatar asked Aug 18 '15 06:08

zulekha


People also ask

What does MongoDB aggregation return?

Returns: A cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation. If the pipeline includes the $out operator, aggregate() returns an empty cursor.

How do I export MongoDB aggregation results?

You can use mongo shell and custom javascript to export whole document in one go, please check $match stage should honor the index. 1. Write your mongodb aggregation query in a javascript file and save it as script. js file.

How do I unwind an object in MongoDB?

The MongoDB $unwind stages operator is used to deconstructing an array field from the input documents to output a document for each element. Every output document is the input document with the value of the array field replaced by the element. Points to remember: If the value of a field is not an array, db.

Can we use $and in aggregate MongoDB?

You can use $and with aggregation but you don't have to write it, and is implicit using different filters, in fact you can pipe those filters in case one of them needs a different solution.


1 Answers

Well you can always use $$ROOT providing that your server is MongoDB 2.6 or greater:

db.items.aggregate([
    { '$match': {'restaurant': ObjectId("551111450712235c81620a57")}},
    { '$group':{
        _id : {
            type : '$type',
            category : '$category'
       },
       id: { '$push': '$$ROOT' },
    }}
])

Which is going to place every whole object into the members of the array.

You need to be careful when doing this as with larger results you are certain to break BSON limits.

I would suggest that you are trying to contruct some kind of "search results", with "facet counts" or similar. For that you are better off running a separate query for the "aggregation" part and one for the actual document results.

That is a much safer and flexible approach than trying to group everything together.

like image 184
Blakes Seven Avatar answered Sep 25 '22 15:09

Blakes Seven