return in the db form a field sum and also other collection values to include in the array

Asked

Viewed 42 times

0

I want to create a query in mongodb where it returns the total value of all the houses of a certain type, and also which street of these houses. At the moment I’m just making a query that brings the total value of houses of a certain type.

As I do to include one more field, this field should not be part of the sum or anything, it is just one more item I need in my array, follow the code I have done so far, which will be necessary to include?

I am using the native driver of Mongo.

var homesToRent = []
homesToRent = await mongodb(homesRent).aggregate([
    {
        $group: {
          _id: '$tipo',
          count: { $sum: '$valor' }
        }
      }
])

With return I would like to have including the countryside streets. For example would this:

[
 {_id: 'casas grandes', count: 9650, ruas: ['rua legal',' rua do teste', 'rua bacana'] },
 {_id: 'casas normais', count: 450, ruas: ['outra rua', 'rua dos bobos']}
]
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.