質問

I have the following table structure:

Rentals:

{
    '_id' : '1',
    'user_id' : 1,
    'item_id' : 6,
    'name' : "First rental",
}
{
    '_id' : '2',
    'user_id' : 2,
    'item_id' : 7,
    'name' : "Second rental",
}
{
    '_id' : '3',
    'user_id' : 2,
    'item_id' : 8,
    'name' : "Third rental",
}

I would like a list of rentals grouped by users like this:

{
    'user' : '1',
    'rental_count': 1,
    'rentals' : [
        {
          '_id' : '1',
          'item_id' : 6,
          'name' : "First rental"
        }
    ]
    'user' : '2',
    'rental_count: 2',
    'rentals' : [
        {
          '_id' : '2',
          'item_id' : 7,
          'name' : "Second rental"
        },
        {
          '_id' : '3',
          'item_id' : 8,
          'name' : "Third rental"
        },
    ]
}

How can you do that with mongodb (I can use the aggregation framework if necessary).

I've tried something like this:

self.collection.aggregate([
    {'$group' => {_id: {user_id: '$user_id'},
      rental_items: {'$addToSet' => '$item_id'}
      rental_ids: {'$addToSet' => '$_id'}
    }
]},

But it will never work, because I have the rental information in lots of different sets, and I want all rentals in a single array for each user.

役に立ちましたか?

解決

That aggregate query you posted seems about right, only you'd have to pass an object to $addToSet:

db.rentals.aggregate({ "$group" : 
       { _id: "$user_id", 
         rental_count : {$sum : 1}, 
         rentals : {'$addToSet' : 
             { "item_id" : '$item_id', 
               "rental_id" : "$_id", 
               "name" : "$name" 
             } 
} } } );

Given your sample data, this results in

{
        "result" : [
                {
                        "_id" : 2,
                        "rental_count" : 2,
                        "rentals" : [
                                {
                                        "item_id" : 8,
                                        "rental_id" : "3",
                                        "name" : "Third rental"
                                },
                                {
                                        "item_id" : 7,
                                        "rental_id" : "2",
                                        "name" : "Second rental"
                                }
                        ]
                },
                {
                        "_id" : 1,
                        "rental_count" : 1,
                        "rentals" : [
                                {
                                        "item_id" : 6,
                                        "rental_id" : "1",
                                        "name" : "First rental"
                                }
                        ]
                }
        ],
        "ok" : 1
}

This doesn't have the exact naming you desired, but changing that is the easiest part and I don't want to steal all the fun :-)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top