Building on the response from cirrus, I recommend doing the $unwind
first to avoid the redundant $match
. Something like:
db.books.aggregate(
{$unwind:"$indexTokens"},
{$match:{indexTokens:/^Par/}},
{$group:{_id:null,indexTokens:{$push:"$indexTokens"}}
})
How do you do this in Java? You can use the DBCollection.aggregate(...) method of the MongoDB v2.9.0 driver. Each pipeline operator, eg. $unwind
or $match
, corresponds to a DBObject
object.