Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-585

Error creating a view based on agg pipeline which uses $function operator

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 0.8.0
    • Affects Version/s: 0.7.7
    • Component/s: Aggregation pipeline
    • Labels:
      None
    • 2
    • Not Needed
    • Iteration Vegetable

      [the following works perfectly in the [legacy] Mongo Shell 4.4 for MongoDB 4.4]

      mongosh throws an error when attempting to create a view based on agg pipeline which uses $function operator. The view can't be created as a result.

      Docs for $function: https://docs.mongodb.com/manual/reference/operator/aggregation/function/

      (issue may also affect $accumulator https://docs.mongodb.com/manual/reference/operator/aggregation/accumulator/ too - I've not tested that). 

      Steps to reproduce (using mongosh 0.7.7 connected to a MongoDB 4.4+ DB running on localhost):

      db.persons.drop();
      
      db.persons.insertOne({
          'name': 'John Smith',
      });
      
      db.persons.find();
      
      var pipeline = [
          {'$set': {
              'name_md5': {'$function': {'lang': 'js', 'args': ['$name'], 'body':                   
                  function(val) {
                      return hex_md5(val);
                  }
              }},
          }},
      ];
      
      db.persons.aggregate(pipeline);
      
      db.persons_view.drop();
      
      db.createView('persons_view', 'persons', pipeline);   // ERROR!!!!
      
      db.persons_view.find();
      
      

      When you run this in mongosh, the createView() command above (penultimate line of code) results in following mongosh error being displayed:

      MongoError: Invalid pipeline for view testdata.persons_view :: caused by :: Invalid $set :: caused by :: The body function must be specified.
      

      It seams somehow that mongosh is losing the 'body' field from the $function declared in the pipeline.

       

            Assignee:
            anna.henningsen@mongodb.com Anna Henningsen
            Reporter:
            paul.done@mongodb.com Paul Done
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: