[SERVER-974] $subset query operator Created: 06/Apr/10  Updated: 06/Dec/22  Resolved: 09/Mar/18

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

Type: New Feature Priority: Major - P3
Reporter: Andrea Fontana Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 14
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-6399 Refactor update() code Closed
depends on SERVER-9366 Expose projection (aggregation) opera... Closed
Duplicate
is duplicated by SERVER-16896 Provide "is subset" array functionali... Closed
Related
is related to SERVER-737 $allOrdered / $contains query operati... Closed
Assigned Teams:
Query
Participants:

 Description   

Example collection:

{ test: [1,2,3] }
{ test: [1,3] }
{ test: [2] } 
{ test: [3,4] }

There's no way to get element that match a subset of an array.
It would be useful to write something like:

db.coll.find({ test: {$subset: [1,2,3] }})

to extract all object except the last one (that contains '4'). It's something similar to a reversed $all operator...



 Comments   
Comment by Asya Kamsky [ 09/Mar/18 ]

Now that 3.6 has $expr this can be done via $setIsSubset expression in query.

Comment by Asya Kamsky [ 05/Feb/18 ]

There are two ways to do this currently:

// pre 3.6, find all documents where it's not case that test element is something other than 1, 2 or 3 (double negation)
db.subset.find({$nor:[{test:{$elemMatch:{$nin:[1,2,3]}}}]},{_id:0})
{ "test" : [ 1, 2, 3 ] }
{ "test" : [ 1, 3 ] }
{ "test" : [ 2 ] }
// in 3.6 using aggregation expression
db.subset.find({$expr:{$setIsSubset:["$test",[1,2,3]]}},{_id:0})
{ "test" : [ 1, 2, 3 ] }
{ "test" : [ 1, 3 ] }
{ "test" : [ 2 ] }

Comment by Nick Gerner [ 17/Feb/11 ]

any movement on support for this?

Comment by Michael Stephens [ 07/Aug/10 ]

The included test case (jstests/subset.js) does still pass with an indexed field. I've never delved into mongo's query optimization code, but I'll try to see if I can get a little understanding of it and how to make $subset work with it (if it's not already) later.

Comment by Eliot Horowitz (Inactive) [ 07/Aug/10 ]

The biggest problem with the patch is that it doesn't work with the query optimizer.
So unless I'm missing something, if the field is indexed, its not going to work.

Comment by Michael Stephens [ 07/Aug/10 ]

Any comments on my patch?

Comment by Michael Stephens [ 29/May/10 ]

I took an initial stab at implementing this, see http://github.com/mikejs/mongo/commit/5555757d0354a9991ec8fe4bae498d2bd69a250d – I'm sure it can be improved on, but it works on the supplied test cases.

Generated at Thu Feb 08 02:55:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.