Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to say... match when field is a number... in mongodb?

Tags:

python

mongodb

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 records...

db.zips.aggregate([{$project : {city:{$substr:["$city",0,1]}}},{$sort : {city : 1}} ])

I need to modify this line to display only the records with a city who has a name that is a number (2,3,4, etc)....I think I can use '$match', but how?

db.zips.aggregate([{$project : {city:{$substr:["$city",0,1]}}},{$sort : {city : 1}}, {$match:{???what_to_say_here???} ]) 

How to say 'match when city is a number'?

the out put I get looks like this...

    {
        "city" : "A",
        "_id" : "04465"
    },
    {
        "city" : "1",
        "_id" : "02821"
    },
    {
        "city" : "0",
        "_id" : "04689"
    }

I'm trying to display only the records with a numeric string...this is related to a larger "homework" problem but I can't even get to the actual homework question until I get past this point.

like image 751
thefonso Avatar asked Nov 16 '12 02:11

thefonso


People also ask

What is regex in MongoDB?

Definition. $regex. Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. "PCRE" ) version 8.42 with UTF-8 support.

What does $match do in MongoDB?

The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.

How can check data type field in MongoDB?

As described above, the $type operator works on the BSON type in MongoDB, and it offers two identifiers for each BSON type; one is “integer” and the other is “string“. For instance, to locate a Double data type, one can use integer value “1” and a string “double” to locate the Double data type in the specified field.


1 Answers

Use the $type operator in your $match:

db.zips.aggregate([
    {$project : {city:{$substr:["$city",0,1]}}},
    {$sort : {city : 1}}, 
    {$match: {city: {$type: 16}}}      // city is a 32-bit integer
]);

There isn't a single type value for number so you need to know which type of number you have:

32-bit integer   16
64-bit integer   18
Double           1

Or use an $or operator to match all types of numbers:

db.zips.aggregate([
    {$project : {city:{$substr:["$city",0,1]}}},
    {$sort : {city : 1}}, 
    {$match: {$or: [{city: {$type: 1}}, {city: {$type: 16}}, {city: {$type: 18}}]}}
]);

Or even use $not to match all docs where city is not a string:

db.zips.aggregate([
    {$project : {city:{$substr:["$city",0,1]}}},
    {$sort : {city : 1}}, 
    {$match: {city: {$not: {$type: 2}}}}      // city is not a string
]);

UPDATED

To match all docs where city is a numeric string you can use a regular expression:

db.zips.aggregate([
    {$project : {city:{$substr:["$city",0,1]}}},
    {$sort : {city : 1}}, 
    {$match: {city: /^\d.*$/}}      // city is all digits
]);
like image 79
JohnnyHK Avatar answered Sep 18 '22 22:09

JohnnyHK