假如我们的APP CRASH数据是这样的:Create Table: CREATE TABLE `t_crash` ( `crash_id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'CRASH ID', `app_id` varchar(255) NOT NULL COMMENT 'APP ID', `app_key` varchar(255) NOT NULL COMMENT 'APP KEY', `device_uuid` varchar(255) DEFAULT NULL COMMENT 'DEVICE UUID', `device_model` varchar(255) DEFAULT NULL COMMENT '手机型号', `app_version` varchar(255) DEFAULT NULL COMMENT 'APP版本', `osVersion` varchar(255) DEFAULT NULL COMMENT '操作系统版本', `app_channel` varchar(255) DEFAULT NULL COMMENT 'APP渠道号', `app_start_time` int(10) DEFAULT NULL COMMENT 'APP启动', `app_crash_time` int(10) DEFAULT NULL COMMENT 'CRASH发生时间', `crash_exception_type` varchar(255) DEFAULT NULL COMMENT 'crash类别', `crash_exception_desc` text COMMENT 'CRASH堆栈', `crash_callstack` text COMMENT '完整的错误栈',,我来为大家讲解一下关于mongodb常用语法和mysql差不多吗?跟着小编一起来看一看吧!

mongodb常用语法和mysql差不多吗(详解mysql)

mongodb常用语法和mysql差不多吗

假如我们的APP CRASH数据是这样的:

Create Table: CREATE TABLE `t_crash` ( `crash_id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'CRASH ID', `app_id` varchar(255) NOT NULL COMMENT 'APP ID', `app_key` varchar(255) NOT NULL COMMENT 'APP KEY', `device_uuid` varchar(255) DEFAULT NULL COMMENT 'DEVICE UUID', `device_model` varchar(255) DEFAULT NULL COMMENT '手机型号', `app_version` varchar(255) DEFAULT NULL COMMENT 'APP版本', `osVersion` varchar(255) DEFAULT NULL COMMENT '操作系统版本', `app_channel` varchar(255) DEFAULT NULL COMMENT 'APP渠道号', `app_start_time` int(10) DEFAULT NULL COMMENT 'APP启动', `app_crash_time` int(10) DEFAULT NULL COMMENT 'CRASH发生时间', `crash_exception_type` varchar(255) DEFAULT NULL COMMENT 'crash类别', `crash_exception_desc` text COMMENT 'CRASH堆栈', `crash_callstack` text COMMENT '完整的错误栈',

我们看个例子:

*************************** 1. row ***************************

我们用MYSQL来统计:

统计设备TOP5

SELECT COUNT(*) as number,device_model FROM xmapp_crash

统计CRASH的48小时内每小时的数据

SELECT count(*) as number,DATE_FORMAT(FROM_UNIXTIME(app_crash_time), '%Y-%m-%d %H') as tFROM xmapp_crashWHERE app_crash_time>UNIX_TIMESTAMP()-3600*48GROUP BY DATE_FORMAT(FROM_UNIXTIME(app_crash_time), '%Y-%m-%d %H')ORDER BY t desc

统计一个月内的数据

SELECT count(*) as number,DATE_FORMAT(FROM_UNIXTIME(app_crash_time), '%Y-%m-%d') as tFROM xmapp_crashWHERE app_crash_time>UNIX_TIMESTAMP()-3600*24*30GROUP BY DATE_FORMAT(FROM_UNIXTIME(app_crash_time), '%Y-%m-%d')ORDER BY t desc

现在我们把数据导入到mongo:

<?php$connectionString = 'mysql:host=localhost;port=3306;dbname=mydb';$username='root';$password='root';$db = new PDO($connectionString, $username, $password);$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);$sql = 'SELECT * FROM t_crash';$statement=$db->prepare($sql);$statement->execute();$results=$statement->fetchAll(PDO::FETCH_ASSOC);$m = new MongoClient("mongodb://localhost:27017");$m_db = $m->test;foreach($results as $key=>$result) { $m_db->app_crash->insert($result);

数据查看一下:

db.app_crash.findOne()

我们来做统计:

统计设备TOP5

db.crash.aggregate( [ { $group: { _id: "$device_model", number: { $sum: 1} } }, { $sort: {"number": -1 }} ])

使用mapreduce

db.crash.mapReduce( function() { emit( this.app_version, 1) }, function(key, values) { return Array.sum(values) },

如果使用单独的group功能

db.crash.group({ key: {device_model: 1},

统计CRASH的时间曲线,48小时内每小时

db.crash.mapReduce( function() {

统计一个月内,机型CRASH的分布

db.crash.mapReduce( function() {

微信公众账号:myersguo

(完)

,