-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Querying
-
Fully Compatible
-
ALL
-
Query 2019-11-04, Query 2019-11-18
-
(copied to CRM)
A $slice and a positional inclusion on different fields do not work correctly together:
c.save( { a:[ 1, 2, 3 ], z:[ 11, 12, 13 ] } ); // The first two elements of 'a' are returned. printjson( c.find( { z:13 }, { a:{ $slice:2 } } ).toArray() ); // The third element of 'a' is returned (incorrectly), because the third element of 'z' is matched. printjson( c.find( { z:13 }, { a:{ $slice:2 }, 'z.$':1 } ).toArray() );
There's also a case where projecting a field of a subobject nested within an array works incorrectly if a positional projection is used on another field. Array field selection should be limited to inclusion fields with a positional operator.
> c.save( { a:[ { b:1, c:2 }, { b:3, c:4 } ], z:[ 11, 12, 13 ] } ) // Expected projection for a.b:1. > c.find( { z:12 }, { 'a.b':1 } ) { "_id" : ObjectId("503aeb6760273eae2b9d57f7"), "a" : [ { "b" : 1 }, { "b" : 3 } ] } // In this case, the positional match is applied to the 'a' array as well as the 'z' array. > c.find( { z:12 }, { 'a.b':1, 'z.$':1 } ) { "_id" : ObjectId("503aeb6760273eae2b9d57f7"), "a" : [ { "b" : 3, "c" : 4 } ], "z" : [ 12 ] } // In this case, the query matched 'z' index 2, which does not exist in the 'a' array, and an error is triggered. > c.find( { z:13 }, { 'a.b':1, 'z.$':1 } ) error: { "$err" : "positional operator element mismatch", "code" : 16353 }
There are other cases where a nested inclusion may conflict with a positional inclusion. In cases like that we should probably uassert:
> c.save( { a:[ { b:1, c:2 }, { b:3, c:4 } ] } ) > c.find( { 'a.b':3 }, { 'a.b':1 } ) { "_id" : ObjectId("503af2d560273eae2b9d57f9"), "a" : [ { "b" : 1 }, { "b" : 3 } ] } // In this case the a.$ currently supersedes the a.b. But we might want to uassert instead. > c.find( { 'a.b':3 }, { 'a.b':1, 'a.$':1 } ) { "_id" : ObjectId("503af2d560273eae2b9d57f9"), "a" : [ { "b" : 3, "c" : 4 } ] }
- is duplicated by
-
SERVER-5247 Support updating more than one array with the positional operator ($)
- Closed
- is related to
-
SERVER-15164 positional operator element mismatch when using both $slice and $ projections
- Closed
- related to
-
SERVER-831 Positional Operator Matching Nested Arrays
- Closed
-
SERVER-6866 Support dotted field past positional operator with positional operator projection
- Backlog
-
SERVER-6399 Refactor update() code
- Closed