dsxml2169 2015-02-16 16:02 采纳率: 0%
浏览 103
已采纳

使用PHP MongoClient find()查询限制返回的字段

I am writing a PHP MongoClient Model which accesses mongodb that stores deploy logs with gitlab information, server hosts, and zend restart instructions. I have a mongo Collection called deployAppConfigs. Its document structure looks like this:

{
"_id" : ObjectId("54de193790ded22d1cd24c36"),
"app_name" : "ai2_api",
"name" : "AI2 Admin API",
"app_directory" : "path_to_app",
"app_owner" : "www-data:deployers",
"directories" : [],
"vcs" : {
    "type" : "git",
    "name" : "input/ai2-api"
},
"environments" : {
    "development" : {
        ... 
    },
    "qa" : {
        ...
    },
    "staging" : {
        ...
    },
    "production" : {
        ...
},
"actions" : {
    "post_checkout" : [ 
        "composer_install"
    ]
}

}

Because there are many documents in this collection, I would like to query the entire collection for only the "vcs" sub document and the "app_name". I am able to execute this command in Robomongo's mongo shell with the following find() query:

db.deployAppConfigs.find({}, {"vcs": 1, "app_name": 1})

This returns exactly what I want for each document in the collection:

{
"_id" : ObjectId("54de193790ded22d1cd24c36"),
"app_name" : "ai2_api",
"vcs" : {
    "type" : "git",
    "name" : "input/ai2-api"
}

}

I am having a problem writing a PHP MongoClient equivalent to that mongo shell command. I basically want to make a PHP MongoClient version of this mongo docs example on Limit Fields to Return from a Query I have tried using an empty array to replace the "{}" in the mongo shell command like this, but it hasn't worked:

$query = array (
    array(), 
    array("vcs"=> 1, "app_name"=> 1)
);

All the fields share the vcs.type = "git" so I tried wrote a query that selects all fields in every document based on that shared value. It looks like this:

$query = array (
   "vcs.type" => "git"
);

But this returns the entire document, which is what I want to avoid.

The alternative could be to do a limit projection find() for the first document in the collection and then use the MongoCursor to iterate through the whole collection, but I'd rather not have to do the extra loop if possible.

Essentially, I am asking how to limit the return fields of a find() query to only one subdocument of each document in the entire collection.

  • 写回答

1条回答 默认 最新

  • douqingnao9246 2015-02-16 16:17
    关注

    looks like I was able to find the solution... I will solve the question and leave it up in case it ends up being useful to anyone else.

    What I ended up having to do was alter my MongoClient custom class find() function, which calls the $collection->find() query, to include a $fields parameter.

    Now, the MongoClient->find() query looks like this:

    $collection->find(
        array("vcs.type" => "git"),
        array("vcs" => 1, "app_name" = 1)
    )
    

    Found the answer on the MongoClient::cursor::find() : here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题