我预测我的收藏和回应是
{$project: {
BusinessCode: {$substrCP : ["$Payload", {$sum : ["$start",15]},{$subtract : [{$subtract : ["$end", "$start"]}, 18]}]}}
}
响应:
{
"_id" : UUID("e3c966cd-cf45-4e42-9a0c-c1d49dcd6c4c"),
"BusinessCode" : "8-4-208696-14-1-0-0"
}
:
现在我想拆分BusinessCode并将第一个char放入其他不存在的字段中。使用此命令我可以这样做:
{$project: {
BusinessCode:1 ,
Domain : {$ifNull:[ "$Domain", { $arrayElemAt: [ { $split : ["$BusinessCode", "-"] },0]}]}}
}
此时我的查询是:
{$project: {
BusinessCode: {$substrCP : ["$Payload", {$sum : ["$start",15]},{$subtract : [{$subtract : ["$end", "$start"]}, 18]}]}}
},
{$project: {
BusinessCode:1 ,
Domain : {$ifNull:[ "$Domain", { $arrayElemAt: [ { $split : ["$BusinessCode", "-"] },0]}]}}
}
及其回应:
{
"_id" : UUID("e3c966cd-cf45-4e42-9a0c-c1d49dcd6c4c"),
"BusinessCode" : "8-4-208696-14-1-0-0",
"Domain" : "8"
}
我认为,我有两个独立的项目并不好,但我怎么能将这两个投影合二为一?
答案 0 :(得分:1)
试试$let
运营商。
{"$replaceRoot":{"newRoot":{
"$let":{
"vars":{
"code":{"$substrCP":["$Payload",{"$sum":["$start",15]},{"$subtract":[{"$subtract":["$end","$start"]},18]}]}
},
"in":{
"BusinessCode":"$$code",
"Domain":{"$ifNull":["$Domain",{"$arrayElemAt":[{"$split":["$$code","-"]},0]}]}
}
}
}}}