0

I'm trying to run this exact query in php with mongodb-ODM

db.runCommand ( { distinct: "messages",
                  key: "conversation",
                  query: { conversation: { $in: ["533f28c9211b6f7e448b4567","52cb29b0211b6fd9248b456b"] } }
                } )

How can I transate it with distinct() ? thanks.

Aysennoussi
  • 3,720
  • 3
  • 36
  • 60
  • I'd start by reading your ODM's documentation, whichever it is. It would be nice to know the exact library you're using and any code you've already written to that effect. – Roberto Sep 03 '14 at 02:51

1 Answers1

0

You can see My Implementation :

Define this method that execute javascript Query in mongoDB

/**
     * Execute javascript in mongodb
     *
     * @param string $js    JavaScript function
     *
     * @return array
     */
    protected function executeJs($js)
    {
        // get database name
        $mongoDatabaseName = $this->dm->getConfiguration()->getDefaultDB();
        // get connection


        $m = $this->dm->getConnection();
        // return results, get mongodb client

        return $m->getMongo()
            // select database
            ->selectDB($mongoDatabaseName)
            // execute javasctipt function
            ->command(array(
                // js
                'eval'   => $js,
                // no lock database, while js will be executed
                'nolock' => true,
            ));
    }

Define your String Query

 $_myQuery = 'function() {var messages = [];
                          db.runCommand ( { distinct: "messages",
                                            key: "conversation",
                                            query: { conversation: { $in: ["533f28c9211b6f7e448b4567","52cb29b0211b6fd9248b456b"] } }
                                          }
                                        ).forEach(function(msg){ messages[]= msg });
                            return messages; }';

Finally , execute your Query

$messageCollection = $this->executeJs($_myQuery);
if(isset(messageCollection))
    var_dump(messageCollection['retval']); // it will show result 
Houssem
  • 11
  • 4