Julian Hollmann
Julian Hollmann

Reputation: 2912

MongoRegex in PHP returns wrong results?

i'm having some trouble with my MongoRegex, the query is below:

db.part_instances.find({
    'Part.name' : /Bauteilname/i, 
    'Part.version' : /1/i, 
    'LocationInstanceLinks' : {'$in' : [1001]}, 
    'Part.Manufacturer.name':/Hersteller/i
});

The returning data is the following: As you will notice, only one of the 4 results should match or am I doing something wrong?

[
    {
        "_id": 1211,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname1",
            "id": 1203,
            "sort_name": "bauteilname1",
            "sort_manufacturer": "hersteller2",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "Manufacturer": {
                "name": "Hersteller2",
                "id": "1202"
            },
            "Building": {
                "id": 1001,
                "name": "Gebäude_dl1.admin",
                "location_instance_id": 1001
            }
        },
        "article_number": "",
        "comments": "",
        "deleted": 1,
        "deleted_date": "Thu, 08 Sep 2011 09:57:22 +0200",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": ""
    },
    {
        "_id": 1212,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname",
            "id": 1201,
            "sort_name": "bauteilname",
            "sort_manufacturer": "hersteller",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "Manufacturer": {
                "name": "Hersteller",
                "id": "1201"
            },
            "Building": {
                "id": 1001,
                "name": "Gebäude_dl1.admin",
                "location_instance_id": 1001
            }
        },
        "article_number": "",
        "comments": "",
        "deleted": 0,
        "deleted_date": "Thu, 08 Sep 2011 10:07:36 +0200",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "",
        "in_room_position": "",
        "manufacturer_number": ""
    },
    {
        "_id": 1214,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname_Fenster",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "sort_name": "bauteilname_fenster",
            "sort_manufacturer": "hersteller_fenster",
            "Manufacturer": {
                "name": "Hersteller_Fenster",
                "id": 1204
            },
            "Building": {
                "location_instance_id": 1001,
                "name": "Gebäude_dl1.admin",
                "id": 1001
            },
            "id": 1206
        },
        "ServicecartLinks": [],
        "article_number": "",
        "comments": "",
        "deleted": 1,
        "deleted_date": "Thu, 08 Sep 2011 10:07:36 +0200",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": "",
        "parents": []
    },
    {
        "_id": 1213,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "Building": {
                "location_instance_id": 1001,
                "name": "Gebäude_dl1.admin",
                "id": 1001
            },
            "Manufacturer": {
                "name": "Hersteller_Glas",
                "id": "1203"
            },
            "buildyear": "",
            "name": "Bauteilname_Glas",
            "sort_manufacturer": "hersteller_glas",
            "sort_name": "bauteilname_glas",
            "status": "local_object",
            "version": "1",
            "id": 1205
        },
        "ServicecartLinks": [],
        "article_number": "",
        "comments": "",
        "deleted": 1,
        "deleted_date": "Thu, 08 Sep 2011 10:07:36 +0200",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": ""
    },
    {
        "_id": 1215,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname2",
            "id": 1207,
            "sort_name": "bauteilname2",
            "sort_manufacturer": "hersteller2",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "Manufacturer": {
                "name": "Hersteller2",
                "id": 1202
            },
            "Building": {
                "id": 1001,
                "name": "Gebäude_dl1.admin",
                "location_instance_id": 1001
            }
        },
        "article_number": "",
        "comments": "",
        "deleted": 1,
        "deleted_date": "Thu, 08 Sep 2011 10:07:36 +0200",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": ""
    },
    {
        "_id": 1216,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname_Fenster",
            "id": 1206,
            "sort_name": "bauteilname_fenster",
            "sort_manufacturer": "hersteller_fenster",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "Manufacturer": {
                "name": "Hersteller_Fenster",
                "id": 1204
            },
            "Building": {
                "id": 1001,
                "name": "Gebäude_dl1.admin",
                "location_instance_id": 1001
            }
        },
        "PartInstanceLinks": [],
        "article_number": "",
        "comments": "",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": ""
    },
    {
        "_id": 1217,
        "Category": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "Craft": {
            "name": "",
            "id": "",
            "building_id": 1001
        },
        "LocationInstanceLinks": [
            1001
        ],
        "MaintenanceTypeLinks": [],
        "Part": {
            "name": "Bauteilname_Glas",
            "id": 1205,
            "sort_name": "bauteilname_glas",
            "sort_manufacturer": "hersteller_glas",
            "version": "1",
            "buildyear": "",
            "status": "local_object",
            "Manufacturer": {
                "name": "Hersteller_Glas",
                "id": "1203"
            },
            "Building": {
                "id": 1001,
                "name": "Gebäude_dl1.admin",
                "location_instance_id": 1001
            }
        },
        "PartInstanceLinks": [],
        "article_number": "",
        "comments": "",
        "ean_number": "",
        "following_product": "",
        "implementation_date": "08.09.2011",
        "in_room_position": "",
        "manufacturer_number": ""
    }
]

Upvotes: 2

Views: 679

Answers (1)

Paul Walls
Paul Walls

Reputation: 6054

I don't know MongoRegex, but considering regex patterns in general:

/Bauteilname/i means to find any case insensitive instances of the word. So that means it will match:

  • bauteilname
  • Bauteilname_Fenster
  • My_Bauteilname_Is_Better_Than_Your_Bauteilname

Ditto for the other patterns. All of your patterns look like they match all the fields in the data sets you've listed.

Give /^Bauteilname$/i a try to match the exact word.

Upvotes: 1

Related Questions