Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-35471

Aggregation $redact error

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • 3.4.13
    • Aggregation Framework
    • None
    • ALL
    • Hide

      // 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
      

      Show
      // 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

    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.

      Attachments

        Activity

          People

            nick.brewer Nick Brewer
            jonah@surveyplanet.com Jonah Werre
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: