-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
114 lines (98 loc) · 3.29 KB
/
db.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/testapp';
// 用于 MongoDB 的数据持久化
function MongoPersistence() {
}
MongoPersistence.prototype.insert = function (data) {
'use strict'
MongoClient.connect(url,function(err,db) {
var insertDocuments = function(db,callback) {
var dbase = db.db('testapp'),
collection = dbase.collection("users");
collection.insert(data,function(err,result) {
callback(result);
});
};
insertDocuments(db,function() {
db.close();
});
});
};
MongoPersistence.prototype.find = function (queryOptions, queryCallback) {
'use strict'
MongoClient.connect(url,function(err,db) {
var findDocuments = function(db, query, callback) {
var dbase = db.db('testapp'),
collection = dbase.collection("users");
collection.find(query).toArray(function(err,docs) {
callback(docs);
});
};
findDocuments(db,queryOptions,function(result) {
db.close();
queryCallback(result);
});
});
};
// 更新设备,例如修改密码,房间变更,查找指定的设备,更新指定的设备数据等
MongoPersistence.prototype.update = function (findQuery, updateQuery) {
'use strict'
MongoClient.connect(url,function(err,db) {
var updateDocument = function(db,callback) {
var dbase = db.db('testapp'),
collection = dbase.collection("users");
console.log(findQuery);
console.log(updateQuery);
collection.update(findQuery, updateQuery, function(err,result) {
callback();
});
};
updateDocument(db,function() {
db.close();
});
});
};
// 添加设备
MongoPersistence.prototype.addDevices = function (data) {
'use strict'
MongoClient.connect(url,function(err,db) {
var updateDocument = function(db,callback) {
var dbase = db.db('testapp'),
collection = dbase.collection("users");
console.log("data" + data.devices);
collection.update({"user_id": data.user_id},{$push:{devices:data.devices}},function(err,result) {
callback();
});
};
updateDocument(db,function() {
db.close();
});
});
};
// 查找指定的(房间设备/设备id),并返回指定的数据
MongoPersistence.prototype.findOrder = function (queryOptions, queryCallback) {
'use strict'
MongoClient.connect(url,function(err,db) {
var findDocuments = function(db, queryOptions, callback) {
var dbase = db.db('testapp'),
collection = dbase.collection("users"),
query = queryOptions.query,
order = queryOptions.order,
updatestr = "{\"$project\": {\"_id\": \"0\", \"control_id\": \"$control_id\", \"user_id\": \"$user_id\", \"rooms\": \"$rooms\", \"devices\": {\"$filter\": {\"input\": \"$devices\", \"as\": \"devices\", \"cond\": {\"$eq\": [\"$$devices." + order + "\", \"" + query + "\"]}}}}}",
findStr = "{ \"$match\": { \"devices." + order + "\" : \"" + query + "\" } }";
findStr = JSON.parse(findStr);
updatestr = JSON.parse(updatestr);
console.log(findStr);
console.log(updatestr);
collection.aggregate([findStr, updatestr]).toArray(function(err,docs) {
callback(docs);
}) ;
};
findDocuments(db,queryOptions,function(result) {
db.close();
queryCallback(result);
});
});
};
// 删除设备,$unset, 还没写
exports.MongoPersistence = MongoPersistence;