如何在php(codeigniter框架)中执行mongodb查询

时间:2017-07-21 11:25:57

标签: php mongodb codeigniter

这是我的MongoDB查询

db.getCollection('fe_report').aggregate([{
    $match: 
    { date: { $gte: ISODate("2017-07-01T00:00:00.000Z"),
            $lte: ISODate("2017-07-19T00:00:00.000Z")} } },
    {
        $group:
        {
            _id :"$employee_id_fk",

            no_of_kms : {$sum: "$no_of_kms"},
            no_of_orders: {$sum: "$no_of_orders"},
            }
    }
   ]
  )

我需要从PHP执行此查询

2 个答案:

答案 0 :(得分:0)

    config/mongo.php

    $config['mongo_server'] = null;
    $config['mongo_dbname'] = 'mydb';
    libraries/Mongo.php

    class CI_Mongo extends Mongo
    {
        var $db;

        function CI_Mongo()
        {   
            // Fetch CodeIgniter instance
            $ci = get_instance();
            // Load Mongo configuration file
            $ci->load->config('mongo');

            // Fetch Mongo server and database configuration
            $server = $ci->config->item('mongo_server');
            $dbname = $ci->config->item('mongo_dbname');

            // Initialise Mongo
            if ($server)
            {
                parent::__construct($server);
            }
            else
            {
                parent::__construct();
            }
            $this->db = $this->$dbname;
        }
    }
    And a sample controller

    controllers/posts.php

    class Posts extends Controller
    {
        function Posts()
        {
            parent::Controller();
        }

        function index()
        {
            $posts = $this->mongo->db->posts->find();

            foreach ($posts as $id => $post)
            {
                var_dump($id);
                var_dump($post);
            }
        }

        function create()
        {
            $post = array('title' => 'Test post');
            $this->mongo->db->posts->insert($post);
            var_dump($post);
        }
    }

try this it work for u

答案 1 :(得分:0)

您可以使用聚合函数按ID分组字段并计算任何列的总和(仅适用于整数类型)

$from = $from.".000Z"; // if you need to filter by date range
    $to = $to.".000Z"; // from date and to date("2017-07-22 00:00:00.000Z")

    $where = array( array(
        '$match' => array(
            'date' => array(
                '$gte' => new MongoDate(strtotime($from)),
                '$lte' =>new MongoDate(strtotime($to)))
        )
    ),
        array(
            '$group' => array(
                '_id' => '$employee_id_fk', // grouping by ID
                'no_of_kms' => array(
                    '$sum' => '$no_of_kms' ' // summing up the KM's fields for the grouped columns 
                ),
                'no_of_orders' => array(
                    '$sum' => '$no_of_orders
                )
            )
        )
    );

    $data =  $this->maggregate('fe_report',$where); // passing it to the maggregate function

// maggregate function()

public function maggregate($table, $where){
  $mdb = new MongoClient("localhost"); // establishing mongodb connection
  $connect = $m->selectDB("examples")->selectCollection($table);
  $result = $connect->aggregate($where); 
  connect->close();
  return $result;

}