对于背景,我必须在集合中创建一个字段,但前提是另一个已知字段(数组)不是空的。我试图用$and和一些条件来构建一个布尔过滤器:
$set: {
clientsAllowed: {
$cond: {
if: {
$and: [
{
businessAllowed: { $exists: true },
},
{
businessAllowed: { $type: "array" },
},
{
businessAllowed: { $ne: [] },
},
],
},
then: [...],
else: [...],
},
}
}
但我知道这个错误:
无效的$set :引起的::无法识别的表达式'$exists‘
$exists
的格式是否错误?还是$and
的舞台?有什么想法吗?
发布于 2022-03-04 09:43:13
businessAllowed的cond类型就足够了。你不需要检查是否存在。
db.collection.aggregate([
{
$set: {
clientsAllowed: {
$cond: {
if: {
$and: [
{
"$eq": [
{
$type: "$businessAllowed"
},
"array"
]
},
{
$ne: [
"$businessAllowed",
[]
]
}
]
},
then: [],
else: []
}
}
}
}
])
https://stackoverflow.com/questions/71349221
复制相似问题