-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sql.js
More file actions
89 lines (75 loc) · 2.17 KB
/
test_sql.js
File metadata and controls
89 lines (75 loc) · 2.17 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
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
var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/local';
var updateName = function(db, callback) {
db.collection('war').updateOne(
{"playerName" : "player1"},
{
$set: {"playerName": "Jameson"}
}, function(err, results) {
console.log(results);
callback();
});
};
var changeTerritory = function(db, player1, player2, territoryName, callback) {
var territoryJSON = {};
territoryJSON["countryName"] = territoryName;
territoryJSON["soldiers"] = 0;
territoryJSON["tanks"] = 0;
var removeJSON = {};
removeJSON["countryName"] = territoryName;
db.collection('war').updateOne (
{"playerName": player1},
{
$push: {"territories" : territoryJSON}
}, function(err, result) {
db.collection('war').updateOne (
{"playerName": player2},
{
$pull: {'territories' : removeJSON}
}, function (err, results) {
callback();
});
});
}
var updateArmy = function (db, searchJSON, updateJSON, callback) {
console.log("asdasda");
db.collection('war').updateOne (
searchJSON,
{
$inc: updateJSON
}, function(err, results) {
callback();
});
}
mongodb.connect(uri, {server: {auto_reconnect: true}}, function(err, db) {
if(err) {
console.log("Database could not load!");
} else {
/*
var searchJSON = {};
searchJSON["territories.countryName"] = "China";
var updateJSON = {};
updateJSON["territories.$.soldiers"] = 2;
console.log(searchJSON);
console.log(updateJSON);
db.close();
*/
/*
var searchJSON = {};
searchJSON["territories.countryName"] = "China";
var updateJSON = {};
updateJSON["territories.$.soldiers"] = 2;
updateArmy(db, searchJSON, updateJSON, function() {
db.close();
});
*/
/*
updateName(db, function() {
db.close();
});
*/
changeTerritory(db, "player2", "player1", "China", function () {
db.close();
})
}
});