Mongoose how to find all unique properties of an instance and its collection?

I have a collection of pants like this:

{
  color: 'Red',
  'material': 'Cotton',
  size: '33x44'
},
{
  color: 'Red',
  material: 'Cotton',
  size: '38x46'
} // etc.

Is it possible to create a query that returns all of the information that does not change (material and color) and a list of all the variations of properties that do (size).

I am guessing this would look something like:

Shirts.find(sizes, {color: 'Red'}, function (err, sizes) {
  // Just pseudocode here
}).distinct()

But that is just a very rough attempt at pseudocode.

Answers


An aggregation query like the following will give you the unique sizes for the color red grouped by color and material.

Shirts.aggregate([
    {$match: {color: 'Red'}},
    {$group: {_id: {color: '$color', material: '$material'}, sizes: {$addToSet: '$size' }}}
], function (err, result)) {
    console.log(result);
});

The output will be like:

{
    [
        {
            _id: {
                color: 'Red',
                material: 'Cotton'
            },
            sizes: [
                '33x44',
                '34x86'
            ]
        }
    ]
}

Need Your Help

UINavigationController title is not displayed?

ios objective-c uinavigationcontroller

I have normal UIViewController ,inwhich I added UINavigationControllerDelegate,i

How to use 'INSERT INTO IGNORE' with an OR clause or 'NOT EXISTS' in mysql

php mysql pdo

I have a table named as equipment and table schema is as follows