0

Ok, guys. As most of you might know, there is this new MongoDB Driver for PHP 7 out there. I was trying it out. I got stuck at this query here :

There is a collection like this:

{ 
"_id" : ObjectId("592d026e2a5e742704e60055"), 
"name" : "One", 
"conn" : [
    {
        "_id" : ObjectId("592d03cd3c83d5265c004d0b"), 
        "name" : "MongoDB Fans", 
        "comments" : "nice!"
    }, 
    {
        "_id" : ObjectId("592d06513c83d5265c004d17"), 
        "name" : "SQL Fans", 
        "comments" : "not so nice!"
    }
   ]
},
{ 
"_id" : ObjectId("592d0eec2a5e742704e60056"), 
"name" : "Two", 
"conn" : [
    {
        "_id" : ObjectId("592d0ef13c83d5265c004d37"), 
        "name" : "Cassandra Fans", 
        "comments" : "spooky!"
    }
   ]
  }

Now, what I want to achieve is this: 1. Find all "conn" fields (irrespective of which document they belong to) that contain a user input (say "xyz") in "name" as a sub-string. 2. Project the selected "conn" fields out.

I have tried using the following filters and options without success (Almost all of these return all the "conn" subsets instead of only the filtered subsets):

  1. $filters = ["conn.name" => new MongoDB\BSON\Regex($name,'i')]; $options = ["projection" => ["_id" => 0,"conn" =>1];
  2. $filters = ["conn" => ["\$all" =>["name" => new MongoDB\BSON\Regex($name,'i')]]]; $options = ["projection" => ["_id" => 0,"conn" =>1];
  3. $filters = ["conn.name" => '/'.$name.'/i']; $options = ["projection" => ["_id" => 0,"conn" =>1];

NOTE: I am using PHP 7 and the latest mongod PECL extension

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Roy
  • 105
  • 1
  • 10
  • Have you tried something or just asking for ready to use solution? – malarzm Jun 01 '17 at 10:20
  • @malarzm I have tried a bunch of different combinations of filters and options. I have also gone through the mongodb manual on the official site with no luck. I just cannot understand how the filters and options work. I will add all the combinations that I have tried so far. – Roy Jun 01 '17 at 15:13
  • See if this helps. https://stackoverflow.com/questions/35750920/regex-as-filter-in-projection – s7vr Jun 01 '17 at 17:04
  • @Veeram What would be the proper way to use $group and $match in php filters and options? – Roy Jun 01 '17 at 17:21
  • Here is an example which is very similar to what you need. https://stackoverflow.com/questions/42044155/using-aggregate-to-combine-a-list-of-all-subdocuments-that-match-query/42045797#42045797 – s7vr Jun 01 '17 at 17:23
  • Funny, I thought I had searched all over SO for it. Almost exactly similar to what I need. Thanks @Veeram. Also, can you suggest any formal text on this new MongoDB PHP Driver ? AllI could find describe the old Mongo library. – Roy Jun 02 '17 at 03:16
  • Also, why can aggregate not be done using filters? Any idea? – Roy Jun 02 '17 at 03:19
  • You are welcome. These links should help. http://php.net/manual/en/set.mongodb.php & https://docs.mongodb.com/php-library/master/ – s7vr Jun 02 '17 at 03:20
  • Yes the only reason is regex is still not supported in filter cond pipeline. More here https://jira.mongodb.org/browse/SERVER-11947 – s7vr Jun 02 '17 at 03:22
  • Thanks a lot. Saved my day. – Roy Jun 02 '17 at 04:03

0 Answers0