Skip to content

Commit 826e5be

Browse files
committed
Allow passing of options to agg query
1 parent 258905a commit 826e5be

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

dist/server/mongodb-proxy.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ function forIn(obj, processFunc)
183183

184184
function parseQuery(query, substitutions)
185185
{
186+
doc = {}
187+
queryErrors = []
188+
186189
query = query.trim()
187190
if (query.substring(0,3) != "db.")
188191
{
192+
queryErrors.push("Query must start with db.")
189193
return null
190194
}
191-
doc = {}
192-
queryErrors = []
193195

194196
// Query is of the form db.<collection>.aggregate or db.<collection>.find
195197
// Split on the first ( after db.
@@ -200,7 +202,7 @@ function parseQuery(query, substitutions)
200202
}
201203
else
202204
{
203-
// Split the first bit - it's the collection name and operation ( find or aggregate )
205+
// Split the first bit - it's the collection name and operation ( must be aggregate )
204206
var parts = query.substring(3, openBracketIndex).split('.')
205207
// Collection names can have .s so last part is operation, rest is the collection name
206208
if (parts.length >= 2)
@@ -224,8 +226,16 @@ function parseQuery(query, substitutions)
224226
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
225227
if ( doc.operation == 'aggregate')
226228
{
227-
// Arg is pipeline
228-
doc.pipeline = JSON.parse(args)
229+
// Wrap args in array syntax so we can check for optional options arg
230+
args = '[' + args + ']'
231+
docs = JSON.parse(args)
232+
// First Arg is pipeline
233+
doc.pipeline = docs[0]
234+
// If we have 2 top level args, second is agg options
235+
if ( docs.length == 2 )
236+
{
237+
doc.agg_options = docs[1]
238+
}
229239
// Replace with substitutions
230240
for ( var i = 0; i < doc.pipeline.length; i++)
231241
{
@@ -274,10 +284,10 @@ function runAggregateQuery( requestId, queryId, body, queryArgs, res, next )
274284

275285
// Get the documents collection
276286
const collection = db.collection(queryArgs.collection);
277-
logQuery(queryArgs.pipeline)
287+
logQuery(queryArgs.pipeline, queryArgs.agg_options)
278288
var stopwatch = new Stopwatch(true)
279289

280-
collection.aggregate(queryArgs.pipeline).toArray(function(err, docs)
290+
collection.aggregate(queryArgs.pipeline, queryArgs.agg_options).toArray(function(err, docs)
281291
{
282292
if ( err != null )
283293
{
@@ -381,11 +391,17 @@ function logRequest(body, type)
381391
}
382392
}
383393

384-
function logQuery(query, type)
394+
function logQuery(query, options)
385395
{
386396
if (serverConfig.logQueries)
387397
{
398+
console.log("Query:")
388399
console.log(JSON.stringify(query,null,2))
400+
if ( options != null )
401+
{
402+
console.log("Query Options:")
403+
console.log(JSON.stringify(options,null,2))
404+
}
389405
}
390406
}
391407

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grafana-mongodb",
33
"private": true,
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"description": "",
66
"main": "server/mongodb-proxy.js",
77
"scripts": {

server/mongodb-proxy.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ function forIn(obj, processFunc)
183183

184184
function parseQuery(query, substitutions)
185185
{
186+
doc = {}
187+
queryErrors = []
188+
186189
query = query.trim()
187190
if (query.substring(0,3) != "db.")
188191
{
192+
queryErrors.push("Query must start with db.")
189193
return null
190194
}
191-
doc = {}
192-
queryErrors = []
193195

194196
// Query is of the form db.<collection>.aggregate or db.<collection>.find
195197
// Split on the first ( after db.
@@ -200,7 +202,7 @@ function parseQuery(query, substitutions)
200202
}
201203
else
202204
{
203-
// Split the first bit - it's the collection name and operation ( find or aggregate )
205+
// Split the first bit - it's the collection name and operation ( must be aggregate )
204206
var parts = query.substring(3, openBracketIndex).split('.')
205207
// Collection names can have .s so last part is operation, rest is the collection name
206208
if (parts.length >= 2)
@@ -224,8 +226,16 @@ function parseQuery(query, substitutions)
224226
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
225227
if ( doc.operation == 'aggregate')
226228
{
227-
// Arg is pipeline
228-
doc.pipeline = JSON.parse(args)
229+
// Wrap args in array syntax so we can check for optional options arg
230+
args = '[' + args + ']'
231+
docs = JSON.parse(args)
232+
// First Arg is pipeline
233+
doc.pipeline = docs[0]
234+
// If we have 2 top level args, second is agg options
235+
if ( docs.length == 2 )
236+
{
237+
doc.agg_options = docs[1]
238+
}
229239
// Replace with substitutions
230240
for ( var i = 0; i < doc.pipeline.length; i++)
231241
{
@@ -274,10 +284,10 @@ function runAggregateQuery( requestId, queryId, body, queryArgs, res, next )
274284

275285
// Get the documents collection
276286
const collection = db.collection(queryArgs.collection);
277-
logQuery(queryArgs.pipeline)
287+
logQuery(queryArgs.pipeline, queryArgs.agg_options)
278288
var stopwatch = new Stopwatch(true)
279289

280-
collection.aggregate(queryArgs.pipeline).toArray(function(err, docs)
290+
collection.aggregate(queryArgs.pipeline, queryArgs.agg_options).toArray(function(err, docs)
281291
{
282292
if ( err != null )
283293
{
@@ -381,11 +391,17 @@ function logRequest(body, type)
381391
}
382392
}
383393

384-
function logQuery(query, type)
394+
function logQuery(query, options)
385395
{
386396
if (serverConfig.logQueries)
387397
{
398+
console.log("Query:")
388399
console.log(JSON.stringify(query,null,2))
400+
if ( options != null )
401+
{
402+
console.log("Query Options:")
403+
console.log(JSON.stringify(options,null,2))
404+
}
389405
}
390406
}
391407

0 commit comments

Comments
 (0)