在MongoDB中使用投影查询,PHP语法? [英] Query With Projection In MongoDB, PHP Syntax?

查看:86
本文介绍了在MongoDB中使用投影查询,PHP语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将如何执行以下mongodb Shell的工作的php语法是什么?

What is the php syntax which will do the work which the following mongodb Shell does?

> db.SoManySins.find({},{"_id":0,"FactoryCapacity":1})

推荐答案

MongoDB PHP驱动程序函数的名称类似于其外壳程序对应的名称,因此在这种情况下,您将使用

The MongoDB PHP driver functions are named similar to their shell counterparts, so in this case you would be using MongoCollection::find(). The PHP driver uses associative arrays to map fields to MongoDB queries.

由于PHP MongoCollection::find()文档页面当前不包含带投影的示例,为完整性起见,我在下面添加了一个示例:

Since the PHP MongoCollection::find() documentation page doesn't currently include an example with projection, I've added one below for completeness:

<?php
    $m = new MongoClient();
    $db = $m->selectDB('test');
    $collection = new MongoCollection($db, 'SoManySins');

    // Search criteria
    $query = array();

    // Projection (fields to include)
    $projection =  array("_id" => false, "FactoryCapacity" => true);

    $cursor = $collection->find($query, $projection);
    foreach ($cursor as $doc) {
        var_dump($doc);
    }
?>

对于投影规范,可以在mongo shell中使用1/0(包括/排除),或等效的true/false常数.

For the projection spec you can use 1/0 (include/exclude) as in the mongo shell, or equivalent true/false constants.

通过PHP MongoDB驱动程序文档中的教程以及查看其中的一些内容非常值得10gen网站上的存档的演示文稿

It's well worth working through the Tutorial in the PHP MongoDB driver documentation as well as viewing some of the archived presentations on the 10gen web site.

这篇关于在MongoDB中使用投影查询,PHP语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆