假设我有一个trips
集合,我想在其中计算每个status
的{{1}}。状态是根据其他字段计算得出的。
trip
。pending
。in transit
。completed
。如何使用聚合管道来实现这一目标?
示例文档:
billed
答案 0 :(得分:1)
您可以尝试使用此方法,它使用$switch来检查系列中的所有案例,并使用$type检查字段是否存在:
db.trips.aggregate([{
$addFields: {
status: {
$switch:
{
branches: [
{
case: { $eq: [{ $type: '$driver' }, 'missing'] },
then: 'pending'
},
{
case: { $eq: [{ $type: '$driver' }, 'objectId'] },
then: 'in transit'
},
{
case: {
$and: [{ $eq: [{ $type: '$driver' }, 'objectId'] },
{ $eq: [{ $type: '$invoice' }, 'string'] }]
},
then: "completed"
},
{
case: {
$and: [{ $eq: [{ $type: '$driver' }, 'objectId'] },
{ $eq: [{ $type: '$invoice' }, 'string'] },
{ $eq: [{ $type: '$billNumber' }, 'string'] }]
},
then: "billed"
}
],
default: '$status'
}
}
}
}
])
答案 1 :(得分:0)
一个解决方案就是这样:
bLength = 1
cText = vbNullString
fOffset = WorksheetFunction.RandBetween(-255, 255)
While bLength <= aLength
dCharacter = vbNullString
eCharacter = vbNullString
dCharacter = Mid(Cells(2, 1), bLength, 1)
If 0 > (Asc(dCharacter) + fOffset) Then
eCharacter = Chr(Asc(dCharacter) + fOffset + 255)
ElseIf 255 < (Asc(dCharacter) + fOffset) Then
eCharacter = Chr(Asc(dCharacter) + fOffset - 255)
Else
eCharacter = Chr(Asc(dCharacter) + fOffset)
End If
cText = cText + eCharacter
bLength = bLength + 1
Wend
或者使用聚合管道的单个命令(从MongoDB 4.2开始):
db.col.updateMany(
{
driver: { $exists: true },
invoice: { $exists: true }
},
{ $set: {status: "completed"}}
)
db.col.updateMany(
{
driver: { $exists: false },
invoice: { $exists: false }
},
{ $set: {status: "pending"}}
)
db.col.updateMany(
{
driver: { $exists: true },
invoice: { $exists: false }
},
{ $set: {status: "in transit"}}
)