0

I am trying to execute the below command

function(field) {

   //field = '{"'+field+'":{$exists:true}}'; Not working
   //field = tojson(field) Not working
   //field = {"events.concessionDetails":{$exists:true}}; Works if passed as input param


    print(field);
    var cur = db.cssCases.aggregate([

    {$match:{"events.eventType" : {$nin:["FAIL_SYSTEM_SERVICE_CALL"]}}},

    { $unwind: "$events" },

    {$match:field},

    {$group:{_id:{eventType:"$events.eventType"}}},

    { $limit : 100 }

    ])
}

if I pass the below value for the field it works fine.

var field1 = {"events.concessionDetails":{$exists:true}};

But I dont want to pass the '{$exists:true}' and just want to pass events.concessionDetails and form the full command, Below is some code which I would like to execute to accomplish this task.

I tried the following with no success,

tojson(field) tojsonObject(field) eval(field)

Is there any way that we can use to convert a input string to json query to use it in query.

Senthil Arumugam SP
  • 1,461
  • 12
  • 17

1 Answers1

0

Why don't you put the {$exists: true} part in your function code ?

Something like :

var field = "events.concessionDetails" // function argument

var match = {
    $match: {}
}
match.$match[field] = { exists: true }

Then use the match variable when you call the aggregate method.

Antek
  • 1,167
  • 3
  • 10
  • 18