Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:41:24+00:00 2026-06-08T13:41:24+00:00

I have saved regular expressions as a value in collection. I want to check

  • 0

I have saved regular expressions as a value in collection. I want to check a string against that regular expression in mongodb, whether it Is matching or not? I got to know that I should use javascript code for the same but it’s not working from php-mongodb.

It’s working from mongodb client console.

Here My code,

<?php

    $dbName = "XYZ";
    try
    {
            $dbInstanceName = $dbName;
            $mongoInstance  = new Mongo("localhost:27017", array("persist"    => "x"));
            $dbReference = $mongoInstance->$dbInstanceName;
            $db          = $mongoInstance->$dbInstanceName;
    }
    catch (Exception $e)
    {
            echo "<pre>";
            print_r($e);
            echo "</pre>";
            exit;
    }

    $query  = new MongoCode("function (){return RegExp(this.regCountry).test('India');}");

    $c = "Countries1";

    $cursor = $db->$c->find($query);


    if ($cursor->count() == 0)
    {
            echo "<pre>";
            print_r("No Data Found.!");
            echo "</pre>";
            exit;
    }
    else
    {

            $data = array();
            while ($cursor->hasNext())
            {
                    $cursor->next();
                    array_push($data, $cursor->current());
            }                
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;

?>

its working but return nothing….!

resonse :

Array
(

)

here, consoled screen sort, its given return a value.

enter image description here

here my data sample :

enter image description here

What’s wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:41:26+00:00Added an answer on June 8, 2026 at 1:41 pm

    Finally I got Answer :

    change my code shown in below

    <?php
    
        $dbName = "Scrubly";
        try
        {
                $dbInstanceName = $dbName;
                $mongoInstance  = new Mongo("localhost:27017", array("persist"    => "x"));
                $dbReference = $mongoInstance->$dbInstanceName;
                $db          = $mongoInstance->$dbInstanceName;
        }
        catch (Exception $e)
        {
                echo "<pre>";
                print_r($e);
                echo "</pre>";
                exit;
        }
    
        $query = "function(){return db.Countries1.find(function (){return RegExp(this.regCountry).test('India');}).toArray();}";
    
        $result = $db->execute($query);
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        exit;
    
    ?>
    

    output :

    Array
    (
    [retval] => Array
        (
            [0] => Array
                (
                    [_id] => MongoId Object
                        (
                            [$id] => 500e5b456bcb44780d000072
                        )
    
                    [country] => India
                    [countryCode] => IN
                    [createdAt] => 2012-07-24 13:52:29
                    [id] => 386
                    [regCountry] => ^(.*)India(.*)$
                    [updatedAt] => 2012-07-24 13:52:29
                )
    
        )
    
    [ok] => 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check user submitted urls against a regular expression, if the url
I have a couple of regular expressions that work fine using Emacs' interactive search.
I have a string like that : 0d(Hi)i(Hello)4d(who)i(where)540d(begin)i(began) And i want to make it
I have a date which is saved in a regular string. // format =
I have saved a bunch of CGContextRefs and I want to draw all of
I have a kmz file that I have saved as kml to use in
I need to find attribute values in an ASPX file using regular expressions. That
I have a form where users enter a URL. I need a regular expression
In perl, if I have a string $str and a pattern saved in $pat
I need a regular expression find and replace that I'm a little stuck on.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.