Sorry for my English, I use google translate.
I have code:
const r = TestTree.aggregate([{
$match : {
$or : [{
left : {
$gte : left_id,
$lte : right_idp,
},
}, {
right : {
$gte : left_id,
$lte : right_idp,
},
}],
},
}, {
$project : {
title : "$title",
level : {
$sum : ["$level", {
$cond : {
if: {
$and : [{
$gte : ["$left", left_id],
}, {
$lte : ["$left", right_id],
}]
}, then : -(level - 1) + levelp, else : 0
},
}],
},
left : {
$sum : ["$left", {
$cond : {
if: {
$and : [{
$gte : ["$left", right_id],
}, {
$lte : ["$left", right_idp],
}]
}, then : -(right_id - left_id + 1), else : {
$cond : {
if: {
$and : [{
$gte : ["$left", left_id],
}, {
$lte : ["$left", right_id],
}]
}, then : (right_idp - 1 - right_id), else : 0
}
}
},
}],
},
right : {
$sum : ["$right", {
$cond : {
if: {
$and : [{
$gte : ["$right", right_id + 1],
}, {
$lte : ["$right", right_idp - 1],
}]
}, then : -(right_id - left_id + 1), else : {
$cond : {
if: {
$and : [{
$gte : ["$right", left_id],
}, {
$lte : ["$right", right_id],
}]
}, then : (right_idp - 1 - right_id), else : 0
}
}
},
}],
},
}
}])
r.forEach(function (el) {
TestTree.update(el._id, {
$set : {
level : el.level,
left : el.left,
right : el.right,
}
})
})
Is it possible to upgrade a single query?