From 6f595b6fc5e978c170b21edb20c641393ceb8cb1 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Tue, 26 May 2015 12:52:36 +0300 Subject: [PATCH] Fixed ability to query tasks collection using task ID. Fixes #68. --- lib/nbi.coffee | 1 + lib/query.coffee | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/nbi.coffee b/lib/nbi.coffee index e56f97a..794dab9 100644 --- a/lib/nbi.coffee +++ b/lib/nbi.coffee @@ -348,6 +348,7 @@ listener = (request, response) -> else q = {} q = query.expand(q, aliases) if collectionName is 'devices' + q = query.substituteObjectId(q) if collectionName is 'tasks' if urlParts.query.projection? projection = {} diff --git a/lib/query.coffee b/lib/query.coffee index 20664f7..23137e8 100644 --- a/lib/query.coffee +++ b/lib/query.coffee @@ -22,6 +22,7 @@ common = require './common' normalize = require './normalize' +mongodb = require 'mongodb' expandValue = (param, value) -> @@ -103,5 +104,30 @@ expand = (query, aliases) -> return new_query +# Replace _id string values with ObjectID type +substituteObjectId = (query) -> + for k, v of query + if k[0] == '$' # logical operator + for i in [0...v.length] + substituteObjectId(v[i]) + else if k == '_id' + if common.typeOf(v) is common.STRING_TYPE + query[k] = mongodb.ObjectID(v) if v.length == 24 + else if common.typeOf(v) is common.OBJECT_TYPE + for kk, vv of v + switch kk # operator + when '$in', '$nin' + for i in [0...vv.length] + vv[i] = mongodb.ObjectID(vv[i]) if vv[i].length == 24 + when '$eq', '$gt', '$gte', '$lt', '$lte', '$ne' + v[kk] = mongodb.ObjectID(vv) if vv.length == 24 + when '$exists', '$type' then + # ignore + else + throw new Error('Operator not supported') + + return query + exports.expand = expand +exports.substituteObjectId = substituteObjectId