[SERVER-35471] Aggregation $redact error Created: 07/Jun/18  Updated: 27/Oct/23  Resolved: 13/Jun/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.4.13
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jonah Werre Assignee: Nick Brewer
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

// Drop the collections
db.testUsers.drop();
db.testCollection.drop();
 
// Create test data
db.testUsers.insertMany( [
  {
    _id:ObjectId("54570f77968d6e492b0d68b0"),
    email:"john@example.com",
    password:"1z2j890",
    name: {
      first: "John",
      last: "Smith"
    }
  },
  
  {
    _id:ObjectId("54570f77968d6e492b0d68b1"),
    email:"jane@example.com",
    password:"5sc46dr",
    name: {
      first: "Jane",
      last: "Doe"
    }
  }
]);
 
db.testCollection.insert( {
  _id:ObjectId('5b186cc4d1ded8d06a7c3baf'),
  user:ObjectId("54570f77968d6e492b0d68b0"),
});
 
var pipeline = [
    {
      $lookup: {
        from: 'testCollection',
        localField: '_id',
        foreignField: 'user',
        as: 'nested'
      }
    },
    {
      $project: {
        _id: true,
        // name: true, <---- this will cause an error
        email:true,
        role:true,
        nested: true
      }
    },
    {
      $redact: {
        $cond: {
          if: { 
            $eq: [ 
                {
                  $size:"$nested"
                }
              , 
                0
            ] 
          },
          then: "$$DESCEND",
          else: "$$PRUNE"
         }
      }
    }
  ];
 
// This works fine
var status1 = db.testUsers.aggregate(pipeline);
printjson(statistics.result);
 
// try to project the name field
pipeline[1].$project.name = true
 
// this should error
var status2 = db.testUsers.aggregate(pipeline);
 
 
// I ran this in the new aggregate debugger in Mongodb Compass (1.14.0-beta.3) and it output this error:
//   The argument to $size must be an array, but was of type: missing

Participants:

 Description   

I'm getting a strange error where if I $project a specific field into $redact I get an error. If I uncommon the field it runs fine.  Run the attached script and you'll see what I mean.



 Comments   
Comment by Nick Brewer [ 12/Jun/18 ]

Jonah,

What's happening here is that, when $size:"$nested" is a value other than 0, the combination of $$DESCEND and $cond tells the aggregation pipeline to look beyond the current document level to embedded values that were projected, in this case the array value for the name field. When it looks in the name field, it rechecks against your conditional, and throws an error when it finds that the nested field does not exist within the name field’s array.

To retain all results where the size of $nested is 0, and redact all results where it is any other value, you can replace $$DESCEND with $$KEEP. This will prevent the issue of the aggregation pipeline descending into the embedded array.

Regards,
Nick

Generated at Thu Feb 08 04:39:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.