2
Example of structure:
{
"_id" : ObjectId("598d4eb912f28534d80a5820"),
"nome" : "Emilio",
"produtos" : [
{
"_id" : ObjectId("598d4fb912f28534d80a5821"),
"nome" : "produto1"
},
{
"_id" : ObjectId("598d4fb912f28534d80a5822"),
"nome" : "produto2"
}
]
},
{
"_id" : ObjectId("598d4eb912f28534d80a5821"),
"nome" : "Zezinho",
"produtos" : [
{
"_id" : ObjectId("598d4fb912f28534d80a5825"),
"nome" : "produto3"
}
]
}
How do I search only the "products" of all users and return something like this:
{
"_id" : ObjectId("598d4fb912f28534d80a5821"),
"nome" : "produto1"
},
{
"_id" : ObjectId("598d4fb912f28534d80a5822"),
"nome" : "produto2"
},
{
"_id" : ObjectId("598d4fb912f28534d80a5825"),
"nome" : "produto3"
}
only with {$unwind: "$products"}, already returns me separately even being in the same user, nice, but in $project would have to specify one by one, the problem is that adding another field would not be automatic...
– JBarbosa
As I commented in the reply, if you will only list products frequently, give a thought in your schema, maybe putting the products in a separate Collection.
– Jorge C. Bernhard Tautz