[英]How to compare string feild value in aggregation mongodb
在下圖中,我使用的是$project聚合。 我需要比較text feild 是否等於 'A' 或不使用$cond 。 如果此 boolean 條件為真,則應返回 1,否則返回 0。但它顯示Stage must be proper formatted。 其中SEC是包含文本字段的嵌套數組。
{
'answers.Urban_City.text': 1,
SEC_A: {$cond:['answers.SEC.text':['$eq','A'], 1, 0]}
}
示例文檔我有多個這樣的文檔
{
"_id": {
"$oid": "61dea2c1169cd059e2d1a06e"
},
"metaKey": {
"projectId": 7,
"sectionId": 5,
"userId": 5,
"simpleSurveyResponseCode": "58836213476714"
},
"answers": [
{
"Urban_City": {
"text": "Karachi",
"value": "72"
}
},
{
"SEC": {
"text": "A",
"value": "1"
}
}
],
"__v": 0
}
試試這個:
db.collection.aggregate([
{
$project: {
'answers.Urban_City.text': 1,
SEC_A: {
$map: {
input: "$answers",
in: { $cond: [{ $eq: ['$$this.SEC.text', 'A'] }, 1, 0] }
}
}
}
}
])
如果您提供所需的樣品 output,則更改會更高以獲得適當的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.