I have a document in a collection like so:
{
container : {
P39433: 2,
P30213: 4
}
}
The values 'P39433' and 'P30213' are dynamically generated in the program. I can run the following query using the dot notation against mongo directly and I get the desired result
db.collection.findOne({ "container.P00000" : { $exists : false } })
I get the document back. i.e. I only want to get the document if the field does not exist.
My question is how can I run that query in Node using the native driver when the value P00000
is contained in a variable.
My usual solution is to structure the query like below but it does not return a 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);