【发布时间】:2021-10-18 22:11:06
【问题描述】:
Mongo 新手。尝试根据条件对文档的不同子字段进行分组。条件是字段值的正则表达式。好像——
db.collection.aggregate([{
{
"$group": {
"$cond": [{
"upper.leaf": {
$not: {
$regex: /flower/
}
}
},
{
"_id": {
"leaf": "$upper.leaf",
"stem": "$upper.stem"
}
},
{
"_id": {
"stem": "$upper.stem",
"petal": "$upper.petal"
}
}
]
}
}])
使用 api v4.0:文档中的 cond 显示 - { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
我用上面的代码得到的错误是-"Syntax error: dotted field name 'upper.leaf' can not used in a sub object."
阅读后,我尝试使用 $let 重新分配虚线字段名称。但开始遇到各种语法错误,查询中没有明显问题。
还尝试使用 $project 重命名字段,但得到 - Field names may not start with '$'
认为这里的最佳方法是什么?我总是可以在应用程序级别解决这个问题并将我的查询分成两部分,但在 mongo 中本地解决它可能很有吸引力。
【问题讨论】:
标签: mongodb