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

No way to $project out just the _id field in aggregation

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • 01112017-cleanup
    • Affects Version/s: None
    • Component/s: Server
    • Labels:
      None

      The command

      db.example.aggregate([{$project: {_id: 0}}])

      fails because "$project requires at least one output field." Now that $project supports exclusion of other fields, it would be nice if it supports exclusion of just the _id field.

      Interestingly, I can enforce "exclusion mode" by projecting out a field that doesn't exist:

      > db.example.find()
      { "_id" : ObjectId("576d9dc03fcfa43a8b713c74"), "a" : { "a1" : 5, "a2" : 6 }}
      { "_id" : ObjectId("57716feaf6545b026b3dfca1"), "a" : { "a1" : 3, "a2" : 7 }}
      > db.example.aggregate([{$project: {_id: 0, c: 0} }])
      {"a" : {"a1":5, "a2":6 } , "b":4 }
      {"a" : {"a1":3, "a2":7 }}
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            emily.hall Emily Hall
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              7 years, 32 weeks ago