I'm using Mongoose in nodejs. So here is a sample Publisher model:
name: String,
books: [{
title: String,
author: {
name: String,
age: Number
}
}]
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:
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);
})
This seems like it should work according to: https://dba.stackexchange.com/questions/59661/mongodb-distinct-in-an-array
Any and all input you guys have to help me with this would be much appreciated.