3

I have mongo record as follows.

[1] => Array
    (
        [_id] => MongoId Object
            (
            )

        [id] => 195197
        [rec] => Array
            (
                [0] => Array
                    (
                        [id] => 195197
                        [data] => ways to skin a cat
                        [total] => 313
                    )

                 [1] => Array
                    (
                        [id] => 702724
                        [data] => 2010-07-25 15:09:40
                    )
            )

          [rec2] => Array
            (
                [0] => Array
                    (
                        [id] => 195197
                        [data] => ways to skin a cat
                        [total] => 313
                    )

                 [1] => Array
                    (
                        [id] => 702724
                        [data] => 2010-07-25 15:09:40
                    )
            )
    );

I want to search those records from mongo db having rec & rec2 element and having "data" element is in date format through Mongo regular expression. Is any way to find such records?

0

2 Answers 2

5

From the shell you should be able to do something like the following:

db.foo.find( { 'rec.data' : /\d\d\d\d-\d\d-\d\d/ } )

From PHP, you would use the MongoRegex class.

Looks something like this:

$regex = new MongoRegex("/\d\d\d\d-\d\d-\d\d/");
$where = array("rec.data" => $regex);
$cursor = $db->foo->find( $where ); 
Sign up to request clarification or add additional context in comments.

Comments

2

Alternatively regular expressions can be expressed like so:

$where = array("rec.data"=>array('$regex'=>"\d\d\d\d-\d\d-\d\d", '$options'=>""));
$cursor = $db->foo->find( $where );

(i'm not sure if options can be omitted if it is blank so i've left it in)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.