mongodb - Showing newest values in subdocument when finding a document -


given following document structure:

{   _id: "abc123",   name: "some name",   properties: [     {       definition: "temp",       values: [         {           created: 1376061128,           value: 82         },         {           created: 1376062368,           value: 85         },         {           created: 1376062627,           value: 88         }       ]     },     {       definition: "alert",       values: [         {           created: 1376061128,           value: 0         },         {           created: 1376062368,           value: 1         },         {           created: 1376062627,           value: 0         }       ]     }   ] } 

i want able return document "newest" (based on created) values each property so:

{   _id: "abc123",   name: "some name",   properties: [     {       definition: "temp",       values: [         created: 1376062627,         value: 88       ]     },     {       definition: "alert",       values: [         created: 1376062627,         value: 0       ]     }   ] } 

i'm new @ dealing mongodb , map/reduce in general i'm not sure direction should looking at. new data model if there suggestions modifications document model i'd willing listen well.

if values pushed array in timely order (as data suggests), can use: db.coll.find({}, {"properties.values" : {$slice : -1}})

it doesn't guarantee newest. return last item in array. since values array implicitly sorted created work.

since aggregation framework doesn't let unwind nested arrays, if not case (values not being pushed created), have use map reduce think.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -