How to combine $ redact with $ exists in MongoDB

Original question: Failure in mongodb seems unclear to me

Let this document:

{ "_id" : ObjectId("50b59cd75bed76f46522c34e"), "student_id" : 0, "class_id" : 2, "scores" : [ { "type" : "exam", "score" : 57.92947112575566 }, { "type" : "quiz", "score" : 21.24542588206755 }, { "type" : "homework", "score" : 68.19567810587429 }, { "type" : "homework", "score" : 67.95019716560351 }, { "type" : "homework", "score" : 18.81037253352722 } ] } 

I need to compile this document so that only grades that are of the "exam" type are saved.

In the original question, it was suggested to use the method: "type is either an" exam "or does not exist", for example:

 db.grades.aggregate([ { $redact: { $cond: { if: { $or: [ { $eq: [ "$type" , "exam" ] }, { "$type": { $exists: false } } ], then: "$$PRUNE", else: "$$DESCEND" } } } 

But this gives me the error "Unrecognized expression" $ type "I tried changing the" $ type "to" type ", but it shows another error:" Unrecognized expression "$ exists".

I also tried the $ setIntersection method:

 $cond: { if: { $gt: [ { $size: { $setIntersection: [ [{ $ifNull: ["$type", "exam"] }], ["exam"] ] } }, 0 ] }, then: "$$PRUNE", else: "$$DESCEND" } 

But it gives me 0 results.

Any help would be appreciated!

0
source share

All Articles