Mongo find array in collection

3

Good people, this is my output with the 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 returns only the product list of a specific userId.

example:  I want to consult all userId products: o4wQ2i4Dt7cAMNf9A.

    
asked by anonymous 25.08.2016 / 05:17

1 answer

0

This will return a list with the entire arrays for 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 of these possible arrays into a single array, you will need to use Aggregation Framework . There is an example very similar to this problem here .

    
19.09.2016 / 17:47