MongoDBの
let q = {
"condition": "and",
"rules": [
{
"field": "Brief_D_Reactiedatum",
"operator": "!=",
"value": "Eventtoets_Fn"
},
{
"condition": "or",
"rules": [
{
"field": "Alleen_AO",
"operator": "=",
"value": "Parkeerreden"
}
]
}
]
};
const conditions = { "and": "$and", "or": "$or" };
const operators = { "=": "$eq", "!=": "$ne", "<": "$lt", "<=": "$lte", ">": "$gt", ">=": "$gte" };
const mapRule = rule => ({
[operators[rule.operator]]: [ "$"+rule.field, rule.value ]
});
const mapRuleSet = ruleSet => {
return {
[conditions[ruleSet.condition]]: ruleSet.rules.map(
rule => rule.operator ? mapRule(rule) : mapRuleSet(rule)
)
}
};
let mongoDbQuery = { $expr: mapRuleSet(q) };
console.log(mongoDbQuery);
結果式は、MongoDBのfind
に渡すことができます メソッド
db.col.find(mongoDbQuery);
または
db.col.aggregate([{ $match: mongoDbQuery }]);