Brug $type
operator i din $match
:
db.zips.aggregate([
{$project : {city:{$substr:["$city",0,1]}}},
{$sort : {city : 1}},
{$match: {city: {$type: 16}}} // city is a 32-bit integer
]);
Der er ikke en enkelt typeværdi for tal, så du skal vide, hvilken type tal du har:
32-bit integer 16
64-bit integer 18
Double 1
Eller brug en $or
operator til at matche alle typer tal:
db.zips.aggregate([
{$project : {city:{$substr:["$city",0,1]}}},
{$sort : {city : 1}},
{$match: {$or: [{city: {$type: 1}}, {city: {$type: 16}}, {city: {$type: 18}}]}}
]);
Eller endda brug $not
for at matche alle dokumenter hvor city
er ikke en streng:
db.zips.aggregate([
{$project : {city:{$substr:["$city",0,1]}}},
{$sort : {city : 1}},
{$match: {city: {$not: {$type: 2}}}} // city is not a string
]);
OPDATERET
For at matche alle dokumenter hvor city
er en numerisk streng, du kan bruge et regulært udtryk:
db.zips.aggregate([
{$project : {city:{$substr:["$city",0,1]}}},
{$sort : {city : 1}},
{$match: {city: /^\d.*$/}} // city is all digits
]);