db.js
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();