Created
July 13, 2012 14:50
-
-
Save jmikola/3105301 to your computer and use it in GitHub Desktop.
MongoRegex with $all and $in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.foo.find({ name: { $all : [/^c/, /^d/] } }); | |
{ "_id" : ObjectId("5000358de84df15c27000000"), "name" : [ "john", "martin", "chuck", "dave" ] } | |
{ "_id" : ObjectId("5000358de84df15c27000001"), "name" : [ "craig", "don", "dan" ] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$m = new Mongo(); | |
$c = $m->test->foo; | |
$c->drop(); | |
$c->save(['name' => ['john', 'martin', 'chuck', 'dave']]); | |
$c->save(['name' => ['craig', 'don', 'dan']]); | |
var_dump(iterator_to_array($c->find(['name' => ['$all' => [new MongoRegex('/^c/'), new MongoRegex('/^d/')]]]))); | |
var_dump(iterator_to_array($c->find(['name' => ['$in' => [new MongoRegex('/^c/'), new MongoRegex('/^d/')]]]))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(2) { | |
["5000358de84df15c27000000"]=> | |
array(2) { | |
["_id"]=> | |
object(MongoId)#8 (1) { | |
["$id"]=> | |
string(24) "5000358de84df15c27000000" | |
} | |
["name"]=> | |
array(4) { | |
[0]=> | |
string(4) "john" | |
[1]=> | |
string(6) "martin" | |
[2]=> | |
string(5) "chuck" | |
[3]=> | |
string(4) "dave" | |
} | |
} | |
["5000358de84df15c27000001"]=> | |
array(2) { | |
["_id"]=> | |
object(MongoId)#9 (1) { | |
["$id"]=> | |
string(24) "5000358de84df15c27000001" | |
} | |
["name"]=> | |
array(3) { | |
[0]=> | |
string(5) "craig" | |
[1]=> | |
string(3) "don" | |
[2]=> | |
string(3) "dan" | |
} | |
} | |
} | |
array(2) { | |
["5000358de84df15c27000000"]=> | |
array(2) { | |
["_id"]=> | |
object(MongoId)#5 (1) { | |
["$id"]=> | |
string(24) "5000358de84df15c27000000" | |
} | |
["name"]=> | |
array(4) { | |
[0]=> | |
string(4) "john" | |
[1]=> | |
string(6) "martin" | |
[2]=> | |
string(5) "chuck" | |
[3]=> | |
string(4) "dave" | |
} | |
} | |
["5000358de84df15c27000001"]=> | |
array(2) { | |
["_id"]=> | |
object(MongoId)#4 (1) { | |
["$id"]=> | |
string(24) "5000358de84df15c27000001" | |
} | |
["name"]=> | |
array(3) { | |
[0]=> | |
string(5) "craig" | |
[1]=> | |
string(3) "don" | |
[2]=> | |
string(3) "dan" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment