erkpwejropi
erkpwejropi

Reputation: 401

How to access value of variable in Mongo?

var a = "act";
db.s.findOne( {
   BrandId: doc.BrandId, 
   a : {$exists:false} 
} ).a;

I try to pass the value of variable "a" to my MongoScript. But it looks it can not be simply used. anyone can help?

Upvotes: 1

Views: 334

Answers (1)

yaoxing
yaoxing

Reputation: 4173

Build your query step by step and do not use . use [] instead

var a = 'act';
var query = {BrandId: doc.BrandId};
query[a] = {$exists:false};
db.s.findOne(query)[a];

Note:

query.act == query['act'].

Just some javascript tricks.

Upvotes: 3

Related Questions