db.js 1.75 KB
var mongoose = require("mongoose");
mongoose.connect('mongodb://172.16.6.108:27017/app_collect_fulltext');
var Schema = mongoose.Schema;   //  创建模型
var AppDbScheMa = new Schema({
    uid: String,
    os: String
});
var AppDbModel = mongoose.model('col_fulltext', AppDbScheMa); //  与col_fulltext集合关联


var AppDb=function(){};

/**
过滤value为空的键值对
**/
var deleteEmptyKeyValue = function(object) {
	var newobject={};
  	for (var key in object){
      	if (object[key] != '' && object[key] != ' ') {
      		newobject[key] = object[key];
      	};
    }
    return newobject;
}

/**
处理起始时间和结束时间
**/
var processTsValue= function(object) {
	var newobject={};
	if (object['ts_start'] && object['ts_start'] != '' && object['ts_end'] && object['ts_end'] != '') {
		var start_time = Date.parse(object['ts_start'])/1000;
		var end_time = Date.parse(object['ts_end'])/1000;
		object.ts = {$gte :start_time.toString(),$lte:end_time.toString()}
	}else if (object['ts_start'] && object['ts_start'] != '') {
		var start_time = Date.parse(object['ts_start'])/1000;
		object.ts = {$gte :start_time.toString()}
	}else if (object['ts_end'] && object['ts_end'] != '') {
		var end_time = Date.parse(object['ts_end'])/1000;
		object.ts = {$lte :end_time.toString()}
	};
  	for (var key in object){
      	if (key != 'ts_start' && key != 'ts_end') {
      		newobject[key] = object[key];
      	};
    }
    return newobject;
}


/**
按照页面参数对精确查询
**/
AppDb.prototype.findByParams=function(params,callback){
	var newParams = deleteEmptyKeyValue(params);
	newParams = processTsValue(newParams);
	console.log('----<<>>',newParams);
    AppDbModel.find(newParams,function(err,obj){
        callback(err,obj);
    });
}

module.exports=new AppDb();