0

Series is one document. It has an array of series inside it (in this case it has 'Revenge' and 'Raines'). Each series has a cast array with names. And I need a query to get those names.

Who know how can I get a list of all the names from both cast arrays?

My best approach was this query db.series.find( {}, { _id: 0, cast: 1 } ) where a get a cursor with the two cast json arrays.

{ series:
[ 
{ 
  name: 'Revenge',
  user_rating: 7.9,
  duration: 44,
  genres: [ ' Drama', ' Mystery', ' Thriller' ],
  year_start: '2011',
  year_end: '',
  cast: 
   [ { name: 'Madeleine Stowe' },
     { name: 'Emily VanCamp' },
     { name: 'Gabriel Mann' },
     { name: 'Nick Wechsler' },
     { name: 'Henry Czerny' },
     { name: 'Joshua Bowman' },
     { name: 'Christa B. Allen' },
     { name: 'Ashley Madekwe' },
     { name: 'Connor Paolo' },
     { name: 'Barry Sloane' },
     { name: 'Margarita Levieva' } ],
  seasons: [ { number: '3' }, { number: '2' }, { number: '1' } ]

},
{
name: 'Raines',
user_rating: 7.4,
duration: 45,
genres: [ ' Crime', ' Drama' ],
year_start: '2007',
year_end: '',
cast: 
    [ { name: 'Jeff Goldblum' },
     { name: 'Matt Craven' },
     { name: 'Nicole Sullivan' },
     { name: 'Linda Park' },
     { name: 'Dov Davidoff' },
     { name: 'Malik Yoba' },
     { name: 'Madeleine Stowe' } ],
seasons: [ { number: '1' } ] 

}
]
}

I need an output like this:

I need this:
         { name: 'Madeleine Stowe' },
     { name: 'Emily VanCamp' },
     { name: 'Gabriel Mann' },
     { name: 'Nick Wechsler' },
     { name: 'Henry Czerny' },
     { name: 'Joshua Bowman' },
     { name: 'Christa B. Allen' },
     { name: 'Ashley Madekwe' },
     { name: 'Connor Paolo' },
     { name: 'Barry Sloane' },
     { name: 'Margarita Levieva' },
         { name: 'Jeff Goldblum' },
     { name: 'Matt Craven' },
     { name: 'Nicole Sullivan' },
     { name: 'Linda Park' },
     { name: 'Dov Davidoff' },
     { name: 'Malik Yoba' },
     { name: 'Madeleine Stowe' }
4

1 回答 1

0

您可以为此使用聚合框架:

db.series.aggregate( { $unwind : "$series" },
                     { $unwind : "$series.cast" },
                     { $group  : { _id  : "$_id",
                                   cast : {$push:"$series.cast}
                                 }
                     }
);

如果您想将多个演员外观合并为一个,请替换$push$addToSet.

于 2013-10-21T05:18:03.253 回答