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

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)