Wednesday, 10 May 2017

How do I query for distinct values in Mongoose but return all of the doc?

Using mongoose and it's distinct function... how can i combine a find with regex and distict, and return the full document?

Using the distinct function https://docs.mongodb.com/manual/reference/method/db.collection.distinct/#options just returns the unique fields.

This is what i instead cobbled together with lodash..

findByTitle: ( title ) => {
    return new Promise( ( resolve, reject ) => {

        solutionModel.find( {"Title": { $regex: new RegExp( title ,'i') }}, (err, docs) => {
            if( err ) return reject( err );

            return resolve( _.uniqBy(docs, (e) => {
                return e.SolutionID;
            }) );
        });

    } );
},



via John

No comments:

Post a Comment