【问题标题】:Convert console command to ODM将控制台命令转换为 ODM
【发布时间】:2014-10-27 03:09:26
【问题描述】:

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

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

如何使用 distinct() 进行翻译? 谢谢。

【问题讨论】:

  • 我将首先阅读您的 ODM 文档,无论它是什么。很高兴知道您正在使用的确切库以及您已经为此编写的任何代码。

标签: mongodb odm


【解决方案1】:

你可以看到我的实现:

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

/**
     * 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 

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2014-02-23
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2017-08-08
    • 2021-07-25
    相关资源
    最近更新 更多