Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-8973

Allow exclusion in $project stage of aggregation pipeline

    XMLWordPrintable

Details

    • Task
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 3.4.0
    • Server
    • None
    • 0.5
    • KANBAN BUCKET
    • true

    Description

      There is currently no way to easily specify that project stage keep all fields except for a particular one (with exception of _id).

      This can be helpful to limit the amount of data returned when some aggregation also needs to be done.

      Examples

      // Exclude a single field.
      > db.example.insert([
        {_id: 0, a: 1}
      ]);
      > db.example.aggregate([{$project: {a: 0}}])
      {_id: 0}
      > db.example.aggregate([{$project: {a: false}}])  // Means the same thing as above.
      {_id: 0}
       
      // Exclude a nested field.
      > db.example.drop()
      > db.example.insert([
        {_id: 0, a: {b: 1, c: 1}}
      ]);
      > db.example.aggregate([{$project: {'a.b': 0}}])
      {_id: 0, a: {c: 1}}
      > db.example.aggregate([{$project: {a: {b: 0}}}])  // Means the same thing as above.
      {_id: 0, a: {c: 1}}
       
      // Exclude a nested field from an array.
      > db.example.drop()
      > db.example.insert([
        {_id: 0, a: [{b: 1, c: 1}, {b: 2, c: 2}]}
      ]);
      > db.example.aggregate([{$project: {'a.b': 0}}])
      {_id: 0, a: [{c: 1}, {c: 2}]}
      > db.example.aggregate([{$project: {a: {b: 0}}}])  // Means the same thing as above.
      {_id: 0, a: [{c: 1}, {c: 2}]}
      

      Attachments

        Issue Links

          Activity

            People

              allison.moore@mongodb.com Allison Reinheimer Moore
              emily.hall Emily Hall
              Jonathan Dahl Jonathan Dahl
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                5 years, 30 weeks, 4 days ago