0

我正在尝试使用 mongodb-ODM 在 php 中运行这个确切的查询

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

我怎样才能用 distinct() 翻译它?谢谢。

4

1 回答 1

0

你可以看到我的实现:

定义这个在 mongoDB 中执行 javascript Query 的方法

/**
     * 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,
            ));
    }

定义你的字符串查询

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

最后,执行您的查询

$messageCollection = $this->executeJs($_myQuery);
if(isset(messageCollection))
    var_dump(messageCollection['retval']); // it will show result 
于 2015-08-21T21:18:45.067 回答