Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongodb mongoose aggregate two collections using lookup & format the result set.

Pedido.aggregate([
    {
        $match: {} // add your search here
    },
    {
        $lookup: { // this is the alternative to the populate
            from: 'clientes',
            localField: 'cliente',
            foreignField: '_id',
            as: 'clientes'
        }
    },
    {
        $project: { // add all the fields you need from the collection, if you need to omit something from the query results, just don't mention it here
            id: 1,
            clientes: 1,
            date: { $dateToString: { format: "%d/%m/%Y", date: "$date" } } // this will return the date in the format "dd/MM/yyyy"
        }
    }
])
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #mongodb #mongoose #aggregate #collections #lookup #format #result
ADD COMMENT
Topic
Name
9+3 =