[SERVER-30365] How to exclude (sub)fields starting with $ in a query projection Created: 27/Jul/17  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Mattia Borini Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Related
is related to SERVER-14466 Support projecting the $id or $ref fo... Closed
is related to SERVER-8436 Aggregation pipeline cannot process s... Backlog
is related to SERVER-30417 add expression to get value by keynam... Closed
Assigned Teams:
Query Optimization
Participants:

 Description   

I'm querying the oplog.rs collection and I would like to exclude the o.$set field from the results. The 'o.$set': 0 projection gives an error:

"Cannot exclude array elements with the positional operator."

There doesn't seem to be other ways to exclude it. Am I wrong? Is it possible?



 Comments   
Comment by Charlie Swanson [ 27/Jul/17 ]

I don't think there is a way to do it. In versions 3.4.4 and above you can use the aggregation framework and the $objectToArray and $arrayToObject expressions to work around this issue:

db.oplog.rs.aggregate([
  {$project: {
    o: {
      $arrayToObject: {
        $filter: {
          input: {$objectToArray: "$o"},
          cond: {$ne: ["$$this.k", {$literal: "$set"}]}
        }
      }
    }
  }}
])

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