php mongodb manager 查数据的各种姿势

一、连接 mongodb 数据库

 

以下操作默认都是以上面操作为前提(已连接成功)而做的操作

 

二、查的各种姿势

1)通过主键_id来查询,注意:_id不是直接用字符串来表示,要用 new \MongoDB\BSON\ObjectId 对象

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE _id='5a2b9aaf96b4a97efbe31a91';
$id      = new \MongoDB\BSON\ObjectId("5a2b9aaf96b4a97efbe31a91");
$filter  = ['_id' => $id];
$options = [];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

2)查询指定字段(其中_id默认是返回的,如果不想返回,要设置 _id=>0)

复制代码

// mysql: SELECT `source`,`ip` FROM appDownloadRecord WHERE _id='5a2b9aaf96b4a97efbe31a91';
$id      = new \MongoDB\BSON\ObjectId("5a2b9aaf96b4a97efbe31a91");
$filter  = ['_id' => $id];
$options = [
    'projection' => ['_id'=>0, 'source'=>1, 'ip'=>1]
];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

3)普通条件查询

// mysql: SELECT * FROM appDownloadRecord WHERE source='bdsem1' LIMIT 10;
$filter  = ['source' => 'bdsem1'];
$options = ['limit'=>10];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

 

4)范围查询(between)

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE (source='bdsem1') AND (createTime BEWTEEN '2017-12-21 00:00:00' AND '2017-12-28 00:00:00') LIMIT 10;
// mysql: SELECT * FROM appDownloadRecord WHERE (source='bdsem1') AND (createTime>='2017-12-21 00:00:00' AND createTime<'2017-12-28 00:00:00') LIMIT 10;
$startTime = new \MongoDB\BSON\UTCDateTime(strtotime('2017-12-21 00:00:00')*1000);
$endTime   = new \MongoDB\BSON\UTCDateTime(strtotime('2017-12-28 00:00:00')*1000);
$filter  = [
    'source' => 'bdsem1',
    'createTime' => [
        '$gte' => $startTime,
        '$lt'  => $endTime,
    ],
];
$options = ['limit'=>10];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

5)in 查询

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE (source IN ('bdsem1', 'besem2', 'besem3')) LIMIT 10;
$filter  = [
    'source' => [
        '$in' => ['bdsem1', 'bdsem2', 'bdsem3']
    ],
];
$options = ['limit'=>10];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

6)or 操作

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE source='bdsem1' AND (sys='ios' OR sys='other') LIMIT 10;
$filter  = [
    'source' => 'bdsem1',
    '$or' => [
        ['sys'=>'ios'],
        ['sys'=>'other']
    ]
];
$options = ['limit'=>10];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

7)group (聚合)操作

复制代码

// mysql: SELECT sys,sum(*) as total FROM appDownloadRecord WHERE source='bdsem1' GROUP BY sys;
$document = [
    'aggregate' => 'appDownloadRecord',
    'pipeline' => [
        [
            '$match' => [
                'source' => 'bdsem1'
            ]
        ],
        [
            '$group' => [
                '_id' => '$sys',
                'total' => ['$sum'=>1]
            ]
        ]
    ],
    'allowDiskUse' => false,
];
$command = new \MongoDB\Driver\Command($document);
$rows    = $mongo->executeCommand('promotion-prod', $command)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

8)group having

复制代码

// mysql: SELECT sys,sum(*) as total FROM appDownloadRecord WHERE source='bdsem1' GROUP BY sys HAVING total>1000;
$document = [
    'aggregate' => 'appDownloadRecord',
    'pipeline' => [
        [
            '$match' => [
                'source' => 'bdsem1'
            ]
        ],
        [
            '$group' => [
                '_id'   => '$sys',
                'total' => ['$sum'=>1]
            ]
        ],
        [
            '$match' => [
                'total' => ['$gt'=>1000]
            ]
        ]
    ],
    'allowDiskUse' => false,
];
$command = new \MongoDB\Driver\Command($document);
$rows    = $mongo->executeCommand('promotion-prod', $command)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

9)排序 sort

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE source='bdsem1' ORDER BY createTime desc LIMIT 10;
$filter  = ['source' => 'bdsem1'];
$options = [
    'sort'       => ['createTime'=>-1],
    'limit'      => 10
];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

 

10)limit 分页

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE source='bdsem1' LIMIT 10;
$filter  = ['source' => 'bdsem1'];
$options = [
    'limit' => 10
];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

复制代码

// mysql: SELECT * FROM appDownloadRecord WHERE source='bdsem1' LIMIT 10,10;
$filter  = ['source' => 'bdsem1'];
$options = [
    'skip'  => 10,
    'limit' => 10
];
$query   = new \MongoDB\Driver\Query($filter, $options);
$rows    = $mongo->executeQuery('promotion-prod.appDownloadRecord', $query)->toArray();
echo '
';print_r($rows);echo '
';exit;

复制代码

你可能感兴趣的