$sum returning value 0

Asked

Viewed 30 times

2

I created the database and fill in a 'stock' collection as in the attached figure.inserir a descrição da imagem aqui

I was trying to make the sum of the quantity of items in the stock, but Mongo only returns me the value 0 (zero). I used the following command:

db.estoque.Aggregate({$group:{_id:null, sum:{$sum:'quantity'}}})

What I’m doing wrong?

  • Put a "$" before the amount. {$sum:'$quantity'}

1 answer

0

How do you want to use a field of your document in the aggregate, you need to put the prefix $, getting $quantidade.

According to the documentation mongodb:

field path

Path to a field in the Document. To specify a field path, use a string that prefixes the field name with a dollar Sign ($).

Your query will look like this:

db.estoque.aggregate({$group:{_id:null, soma:{$sum:'$quantidade'}}})
  • 1

    Wow, that’s all it was. Fight! the/

Browser other questions tagged

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