如何将 mongoDB shell 查询转换为 php [英] How to convert mongoDB shell query to php

查看:61
本文介绍了如何将 mongoDB shell 查询转换为 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 mongoDB 查询转换为 php

How to Convert mongoDB Query to php

db.getCollection('xyz').aggregate({ $match: { 
$and: [ 
{ created_at: { $gt:new ISODate("2016-07-05"), $lt:new ISODate("2016-09-05") }, id: "1" } 
] 
} }, 
{ $group: { _id : "distance", sum : { $sum: "$distance" } } });

推荐答案

检查下面用于连接 mongodb 和 php 的代码.

Check the code below which used to connect mongodb with php.

$m = new MongoClient();
$db = $m->yourDB;

$collection = $db->yourCollecton;

$sdate = '2016-01-01';
$sdate = '2016-01-31';

$pipeline = array(
    array(
        '$match'=>array('date'=>array('$gte'=>$sdate,'$lte'=>$edate))       
    ),
    array(
         '$group' => array(
            '_id' => array(null),
            'first_field' => array('$field'),
          )
    )
);

$cursor = $collection->aggregate($pipeline);
$row  = $cursor['result'][0];

这篇关于如何将 mongoDB shell 查询转换为 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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