true在我的聚合管道中,如果多個條件匹配,我試圖設定一個值。我稍后會添加更多$and條件,但它甚至不適用于一個條件。
這些是我嘗試過的事情:
{
$set: {
someValue: {
$and: [
{
'ratePlans.maxAdvBookDays': {
$gte: advBookDays,
},
},
],
}
}
}
{
$set: {
someValue: {
$and: [
{
$toBool: {
'ratePlans.maxAdvBookDays': {
$gte: advBookDays,
},
},
}
],
}
}
}
{
$set: {
someValue: {
$cond: {
if: {
$and: [
{
'ratePlans.maxAdvBookDays': {
$gte: advBookDays,
},
},
],
},
then: 'then',
else: 'else',
}
}
}
}
在每種情況下,我都會收到以下錯誤:
MongoServerError: Invalid $set :: 由 :: FieldPath 欄位名可能不包含“.”引起。
但是,當硬編碼布林值時,它似乎確實有效:
{
$set: {
someValue: {
$and: [
true, true, false, true
],
}
}
},
uj5u.com熱心網友回復:
db.collection.aggregate([
{
$set: {
someValue: {
$cond: {
if: {
$and: [
{
$gte: [ "$ratePlans.maxAdvBookDays", 2 ]
}
]
},
then: "then",
else: "else"
}
}
}
}
])
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/444040.html
