So I have a field called \'city\' in my results...the results are corrupted, some times it\'s an actual name, sometimes it\'s a number. The following code displays all the recor
An easier way to match all docs where city
is a numeric string is using the property '0' <= city <= '9'
i.e. your aggregation query becomes:
db.zips.aggregate([
{$project : {city:{$substr:["$city",0,1]}}},
{$sort : {city : 1}},
{$match: {city: {$gte: '0', $lte: '9'}}}
]);
When using JohnnyHK's suggestion of a regex you can save yourself the $project
stage altogether.