I have a collection with the following structure:
{ { node: 'ST20' Ds:{ 699:{ TotCM: 300 Docsis20: 200 Docsis30: 100 } 705:{ TotCM: 250 Docsis20: 150 Docsis30: 100 } } } { node: 'ST21' Ds:{ 651:{ TotCM: 200 Docsis20: 100 Docsis30: 100 } 699:{ TotCM: 100 Docsis20: 0 Docsis30: 100 } } } } As you can see, I have a document that represents a node. Each node, has different channels. My objective is see how many cablemodems I have in each channel. From upfront, I don't know the channels that I have in each node. So I would like to have something like this:
{ node: 'ST20', Ds: 699, TotCM: 300, Docsis20: 200, Docsis30:100 } { node: 'ST20', Ds: 705, TotCM: 250, Docsis20: 150, Docsis30:100 } { node: 'ST21', Ds: 651, TotCM: 200, Docsis20: 100, Docsis30:100 } { node: 'ST21', Ds: 699, TotCM: 100, Docsis20: 0, Docsis30:100 } I tried the following query:
db.statsNodos.aggregate( { $project: {_id:0,Ds:1,node:1}}, { $unwind: "$Ds"} ).pretty() But I get the original document. And, as you can see, they are nested documents and not an array of documents. Maybe I don't have to use $unwind and there is another way to get this. Can you tell me?
$unwindonly works on an arrayIf the operand does not resolve to an array but is not missing, null, or an empty array, $unwind treats the operand as a single element array.So you either need to correct your json (cause it's not actually json) or unwind isn't the way to go here.