For variety, this version converts *string* type that represents ObjectId value to corresponding year-month:
d=[]; o=[]; for (yr=2011; yr < 2018; yr++ ) { for (m=1; m<13; m++) { if (m<10) mo="0"+m; else mo=""+m; var dt=new ISODate(""+yr+"-"+mo+"-01T00:00:00Z"); d.push(""+yr+"-"+mo); /* wrap string in 'new ObjectId()' to convert OID rather than string type */ o.push(""+(dt.getTime()/1000).toString(16)+pad); } } makeLabeledSwitch = function(field, keys, values) { var sw = {$switch:{ "branches":[ ], default:"other"} }; var br=[]; var maxPos=keys.length; var first="<" + keys[0]; br.push({case:{$lt:[field,values[0]]}, then:first}) for (pos = 0; pos < maxPos-1; pos++) { br.push({case:{$lt:[field,values[pos+1]]}, then: keys[pos] }); } var last=">" + keys[maxPos-1]; sw["$switch"]["default"] = last; sw["$switch"]["branches"] = br; return sw; }