How to search by the number from beginning of the numeric key

{
"code": 200,
 "message": "16700 record found",
 "result": [
{
            "liquidation_status": {
                "start": {
                    "date": "16/03/2014",
                    "artical_number": 1050
                },
                "end": {
                    "date": null,
                    "artical_number": null
                }
            },
            "_id": "62d068cb10d394e4b5922874",
            "company_name": "شركة سعيد وعبدالله أبناء علي بن سعيد الناعبي للتجارة",
            "commercail_number": 114586
        },
{
            "liquidation_status": {
                "start": {
                    "date": "16/03/2014",
                    "artical_number": 1050
                },
                "end": {
                    "date": null,
                    "artical_number": null
                }
            },
            "_id": "62d068cb10d394e4b5922875",
            "company_name": "شركة سعيد وعبدالله أبناء علي بن سعيد الناعبي للتجارة",
            "commercail_number": 114486
        },
]
}

i want to search from the beginning of the commercail_number which is an number like 114486
so i want if i search with 11 it will return me all which start from 11

Hi @Mehmood_Zubair - Welcome to the community!

i want to search from the beginning of the commercail_number which is an number like 114486
so i want if i search with 11 it will return me all which start from 11

Based off the single sample document provided, I assume you then want both objects inside of the result array to be returned since they begin with "11". Is this assumption correct?

Additionally, could you also provide the following information:

  1. What version of MongoDB are you using
  2. Sample documents including some where objects with a "commercail_number" value within the "result" do not begin with the specified characters / numbers.
  3. The desired output based off these sample documents.
  4. Does "commercail_number" represent some sort of code and if so, is any arithmetics being performed on it? If not, it may be possible to make this field into a string, so the aggregation below doesn’t need to use $toString, and will thus improve performance
  5. Use case details

In the meantime, if you want to specifically filter out all other results perhaps the following example may help:

db.collection.aggregate(
{
  '$addFields': {
    filteredResults: {
      '$filter': {
        input: '$result',
        cond: {
          '$regexMatch': {
            input: { '$toString': '$$this.commercail_number' },
            regex: /^11/
          }
        }
      }
    }
  }
}

The above would result in document(s) that have an additional field called "filteredResults" which contains the objects within the "result" array that have a "commercail_number" value beginning with "11".

For your reference with regards to the above example:

Another method that may work but will depend on the use case details is possibly a $gte and $lte (E.g. If you’re searching for it to begin with 11 then you could use a $filter for objects within the result array with "commercail_number" values to be $gte 11,000 and $lte 11,999). But again to re-iterate, this will depend on your use case.

As with any of these example code snippets, please test thoroughly in a test environment to verify it meets all your use case(s) and requirements.

Regards,
Jason

4 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.