shell – Mongo find in array within collection


Well folks, this is my output with normal find:

    "_id" : ObjectId("57be6168ce98ad853a96282f"), 
    "nome" : "Mercado Padrão", 
    "userId" : "o4wQ2i4Dt7cAMNf9A", 
    "produtos" : [ 
             "nome" : "coca cola", 
             "vds" : [ 
                 { "desc" : "Sódio", "qtd" : "0,5", "vd" : "mg" } 
              "est" : "100", "preco" : "5.50", "sku" : "KU177" 

My question is how to make a query that only return the list of products from a specific userId.

example: I want to query all products from userId: o4wQ2i4Dt7cAMNf9A.


This will return a list of the entire arrays to the user. If the user has more than one document in the collection, multiple arrays will return:

db.suacolecao.find({userId: "xxxxxxxxx"}, {produtos: 1})

If you need to combine all these possible arrays into a single array, you will need to use the Aggregation Framework . There is a very similar example of this problem here .

Scroll to Top