-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticle3.js
168 lines (147 loc) · 5.78 KB
/
Article3.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* This code was taken from Paul Robert's blog entry at
* http://coding.paulandkana.com/p=12
* Heavily edited to use a different host and database
* and to change the export.saves function
*
* Changes were made by Robert Cochran. Bob can be emailed on
* r2cochran2@gmail.com.
*
*/
// database connection variables
var MongoClient = require('mongodb').MongoClient // Driver for connecting to MongoDB
, format = require('util').format;
var fs = require('fs')
var MongoBinData = require('mongodb').Binary
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var mongoport = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
// Connect to the database
MongoClient.connect(format("mongodb://%s:%s/images?journal=true", host, mongoport), function (err, db) {
if (err) {
throw err
return
}
console.log("Connected to database server on host " + host + ":" + mongoport)
// Query for all images on the database
exports.findAll = function (callback) {
db.collection("demoimages", function (err, collection) {
collection.find( {}, {"_id" : 0 }, { sort: {"fn" : 1 } } )
.toArray(function (err, docs) {
if (err) {
return callback(err, null)
}
else {
docs.forEach(function (entry) {
console.log(entry.fn)
})
callback(null, docs)
}
});
});
};
//Query for images which contain the string 'resized' in the document's
// "im_name" field
exports.findRes = function (callback) {
db.collection("demoimages", function (err, collection) {
collection.find( { "fn" : { $regex: 'resized*' } }, {"_id" : 0 }, { sort: { "fn" : 1 } } )
.toArray(function (err, docs) {
if (err) {
return callback(err, null)
}
else {
docs.forEach(function (entry) {
console.log(entry.fn)
})
callback(null, docs)
}
});
});
};
//Query for all images in the d750 collection.
exports.find750 = function (callback) {
db.collection("d750", function (err, collection) {
collection.find( {}, {"_id" : 0 }, { sort: { "fn" : 1 } } )
.toArray(function (err, docs) {
if (err) {
return callback(err, null)
}
else {
docs.forEach(function (entry) {
console.log(entry.fn)
})
callback(null, docs)
}
});
});
};
//Query for all images in the d650 collection.
exports.find650 = function (callback) {
db.collection("d650", function (err, collection) {
collection.find( {}, {"_id" : 0 }, { sort: { "fn" : 1 } } )
.toArray(function (err, docs) {
if (err) {
return callback(err, null)
}
else {
docs.forEach(function (entry) {
console.log(entry.fn)
})
callback(null, docs)
}
});
});
};
//Query for all images in the d550 collection.
exports.find550 = function (callback) {
db.collection("d550", function (err, collection) {
collection.find( {}, {"_id" : 0 }, { sort: { "fn" : 1 } } )
.toArray(function (err, docs) {
if (err) {
return callback(err, null)
}
else {
docs.forEach(function (entry) {
console.log(entry.fn)
})
callback(null, docs)
}
});
});
};
/*
* The code below (exports.findById) is being tested for
* the demoimages collection of the images database.
*
*/
exports.findById = function (filename, callback) {
console.log("The filename value that was passed is " + filename + "\n")
db.collection("demoimages", function (err, collection) {
collection.find( { "fn" : filename }, { "_id" : 0 }, {} )
.toArray(function (err, docs) {
if (err) {
console.log("A database error occurred in findById\n" + err)
return callback(err, null)
}
else {
// Okay, so we found the image in demoimages. Let us see
// if a smaller resolution image is also in 'd750'.
db.collection("d750", function (err,collection) {
collection.find( { "fn" : { $regex : filename + '*' } }, {"_id" : 0}, { sort: { "fn" : 1 } } )
.toArray(function (err, docs2) {
if (err) {
return callback(err, null)
}
else {
console.log('Image queried from demoimages collection: ' + docs[0].fn)
console.log('Image queried from d750 collection: ' + docs2[0].fn)
console.log('Going to the callback now! ')
callback(null, docs, docs2)
}
})
})
//console.log(docs[0].fn)
//callback(null, docs, docs2)
}
})
});
};
})