[SERVER-19175] add support for $pull with $not Created: 28/Jun/15  Updated: 06/Dec/22  Resolved: 30/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 3.0.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Benjamin M Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File server-19175.js    
Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

> db.version();
3.0.4

Example document:

{
  _id: "...",
  foo: {
    bar: [ 1, null, 3, 4 ],
    baz: [ { value: 1 }, { value: null }, { value: 3 }, { value: 4 } ]
  }
}

I want to remove all values that are $lt: 4 or null. To simplify thing I wanted to use $not: { $gte: 4 }.

This works:

update(
  {},
  { $pull: { 'foo.baz': { 'value': {$not: { $gte: 4 } } } } }
)

And this does not work:

update(
  {},
  { $pull: { 'foo.bar': { $not: { $gte: 4 } } } }
)

It gives the following error:

"writeError" : {
	"code" : 2,
	"errmsg" : "unknown top level operator: $not"
}

Am I doing something wrong or is there another way to do this?



 Comments   
Comment by Asya Kamsky [ 30/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

Examples in this ticket can be done by setting the array to its new value using aggregation array expression $filter which takes arbitrarily complex conditional.

Comment by Benjamin M [ 30/Jun/15 ]

It's not only $not, because $or doesn't work, too. There may be some more expressions.

Comment by Sam Kleinman (Inactive) [ 30/Jun/15 ]

Thanks for the report, I've attached a test case that demonstrates the inconsistency between $lt and $not $gte within $pull expressions, and am passing this on to the query team for additional triage.

Comment by Benjamin M [ 28/Jun/15 ]

You can close this, it's a duplicate of https://jira.mongodb.org/browse/SERVER-19172

Comment by Benjamin M [ 28/Jun/15 ]

Why there's no EDIT button?

In the not working query is a mistake. Should be foo.bar instead of foo.baz :

And this does not work:

update(
  {},
  { $pull: { 'foo.bar': { $not: { $gte: 4 } } } }
)

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