0

in the following image i am using $project aggregation. i need to compare whether text feild is equals to 'A' or not using $cond. If this boolean condition is true it should return 1 otherwise 0. but it is showing Stage must be properly formatted.

{
  'answers.Urban_City.text': 1,
  SEC_A: {$cond:['answers.SEC.text':['$eq','A'], 1,  0]}
}

compass image link

Sample document i have multiple documents like this

{
  "_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
}

I want something like this

db.Sentiments.aggregate(
    { $project: {
        _id: 0,
        Company: 1,
        PosSentiment: {$cond: [{$gt: ['$Sentiment', 0]}, '$Sentiment', 0]},
        NegSentiment: {$cond: [{$lt: ['$Sentiment', 0]}, '$Sentiment', 0]}
    }},
    { $group: {
        _id: "$Company",
        SumPosSentiment: {$sum: '$PosSentiment'},
        SumNegSentiment: {$sum: '$NegSentiment'}
    }});

but at the place of Sentiment i have SEC which is embedded array contains two feilds(String) mentioned in sample document. where i have stuck in $project aggregation if this gets resolved $group would be easy for me.

  • Try `{$cond:[{$eq: ['$answers.SEC.text','A']}, 1, 0]}` Looks like you mixed aggregation operator [$eq](https://docs.mongodb.com/manual/reference/operator/aggregation/eq/) and query operator [$eq](https://docs.mongodb.com/manual/reference/operator/query/eq/) – Wernfried Domscheit Jan 13 '22 at 09:17
  • i used {$cond:[{$eq: ['$answers.SEC.text','A']}, 1, 0]}. but it always return false, nothing gets matched, result in 0 return in $cond. i dont know why. – Haseeb Sheikh Jan 13 '22 at 09:24
  • Please provide same sample input documents. – Wernfried Domscheit Jan 13 '22 at 09:54
  • i have provided sample document in my question – Haseeb Sheikh Jan 13 '22 at 10:25
  • The sample data does not match at all to your aggregation pipeline. What is the desired result? – Wernfried Domscheit Jan 13 '22 at 11:40
  • I want to find the documents of answers.SEC.text: A in project pipeline, These documents in found in $project stage, will get count in the next stage of $group(e.g. group by "answers.Urban_City.text:Karachi") For further clearity you can refer to the following stackoverflow link. But please note in the place of Sentiment i have answers.SEC.text; where SEC is embedded object which contains two feilds (text, value). https://stackoverflow.com/questions/14102596/conditional-sum-in-mongodb – Haseeb Sheikh Jan 13 '22 at 12:28

1 Answers1

0

Try this one:

db.collection.aggregate([
   {
      $project: {
         'answers.Urban_City.text': 1,
         SEC_A: {
            $map: {
               input: "$answers",
               in: { $cond: [{ $eq: ['$$this.SEC.text', 'A'] }, 1, 0] }
            }
         }
      }
   }
])

If you provide a desired sample output, the change will be much higher to receive an appropriate answer.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110