Using mongoose to get the distinct values of an object in array for all documents
<p>I'm using Mongoose in nodejs. So here is a sample Publisher model:</p>

<pre><code>name: String,
books: [{
    title: String,
    author: {
        name: String,
        age: Number
    }
}]
</code></pre>

<p>I want to get all the distinct author names that match a regex. I'm new to noSql so maybe I'm just doing something dumb. I figure I want to do something like:</p>

<pre><code>Publisher.aggregate([
    {$unwind: '$books'},
    {$match: {
        'books.author.name': new RegExp(req.params.regex)
    }},
    {$group: {
        _id: '$books.author.name'
    }},
    {$group: {
        distinctValues: {$addToSet: '$_id'}
    }}
], function(err, result) {
    if(err) throw err;
    res.json(result);
})
</code></pre>

<p>This seems like it should work according to: <a href="https://dba.stackexchange.com/questions/59661/mongodb-distinct-in-an-array">https://dba.stackexchange.com/questions/59661/mongodb-distinct-in-an-array</a></p>

<p>Any and all input you guys have to help me with this would be much appreciated.</p>