[英]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.