1

Can anyone help me get results similar to this?

{ "details" : { "name" : "John Doe" }, "qualifications" : [ "ENG", "DATA", "SW" ] }

{ "details" : { "name" : "Jane Doe" }, "qualifications" : "None" }

The first part is I am trying to use aggregate to match where the field details exists are true. To return equivalent results to db.docs.find({details:{$exists:true}})

The last part is that I am trying to use $cond to return data in a field called qualifications if the field qualifications exists, otherwise result should be None.

  • For the $cond, where qualifications $exists":true, to return data from the field qualifications (e.g. [ "ENG", "DATA", "SW" ]), I am trying prefixing qualifications with $, $qualifications, otherwise answer will be none

The made up field title for qualifications $cond , is spelled same way as qualifications field in database.

I am getting the error Unrecognized expression '$exists' , it seems to me that the syntax for this is not correct when used in the aggregate

db.docs.aggregate([
  {
    $match: {details:{$exists:true}}
  },

  {
    $project: {
      _id: 0,
      "details.name": 1,
      qualifications: {
        $cond: { if: { qualifications: {"$exists":true}}, then: "$qualifications", else: "None" }
      }
    }
  }
]);

Thanks in advance

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
Christopher
  • 427
  • 1
  • 8
  • 18

0 Answers0