node.js - using $exists in Mongo with dynamic key names and the native driver for node -
i have document in collection so:
{ container : { p39433: 2, p30213: 4 } }
the values 'p39433' , 'p30213' dynamically generated in program. can run following query using dot notation against mongo directly , desired result
db.collection.findone({ "container.p00000" : { $exists : false } })
i document back. i.e. want document if field not exist.
my question how can run query in node using native driver when value p00000
contained in variable.
my usual solution structure query below not return result.
var fieldname = 'p00000'; var dynobj = {}; dynobj[fieldname] = { $exists : false }; db.collection.findone( { "container" : dynobj });
you're close. try this:
var fieldname = 'p00000'; var dynobj = {}; dynobj["container." + fieldname] = { $exists: false }; db.collection.findone(dynobj);
update
now node.js 4+ supports computed property names, can create dynobj
in 1 step as:
var dynobj = { ["container." + fieldname]: { $exists: false } };
Comments
Post a Comment