Skip to content

Instantly share code, notes, and snippets.

@silgon
Created March 20, 2018 14:04
Show Gist options
  • Select an option

  • Save silgon/9d202c4046ba9a6d301c05e16a44b98b to your computer and use it in GitHub Desktop.

Select an option

Save silgon/9d202c4046ba9a6d301c05e16a44b98b to your computer and use it in GitHub Desktop.
mongo aggregation unwind example
db.t4.insert({
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"list" : [
{
"a" : ISODate("2012-12-19T06:01:17.171Z"),
"b" : 5
},
{
"a" : ISODate("2013-12-19T06:01:17.171Z"),
"b" : 5
},
{
"a" : ISODate("2014-12-19T06:01:17.171Z"),
"b" : 5
},
{
"a" : ISODate("2015-12-19T06:01:17.171Z"),
"b" : 10
},
{
"a" : ISODate("2016-12-19T06:01:17.171Z"),
"b" : 5
}
]
})
db.t4.aggregate(
{ $match: {_id: ObjectId("512e28984815cbfcb21646a7")}},
{ $unwind: '$list'},
{ $match: {'list.a': {$gt: ISODate("2013-12-19T06:01:17.171Z")}}},
{ $group: {_id: '$_id', list: {$push: {"val":'$list.a',"b":"$list.b"}}}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment