Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Only $slice outer arrays when nested. SERVER-1085

  • Loading branch information...
commit 9e6121b6cb59312f3106b55a2b8c4852cedcbefd 1 parent c64d1ed
@RedBeard0531 RedBeard0531 authored
Showing with 19 additions and 5 deletions.
  1. +4 −4 db/queryutil.cpp
  2. +1 −1  db/queryutil.h
  3. +14 −0 jstests/slice1.js
View
8 db/queryutil.cpp
@@ -760,9 +760,9 @@ namespace mongo {
}
//b will be the value part of an array-typed BSONElement
- void FieldMatcher::appendArray( BSONObjBuilder& b , const BSONObj& a ) const {
- int skip = _skip;
- int limit = _limit;
+ void FieldMatcher::appendArray( BSONObjBuilder& b , const BSONObj& a , bool nested) const {
+ int skip = nested ? 0 : _skip;
+ int limit = nested ? -1 : _limit;
if (skip < 0){
skip = max(0, skip + a.nFields());
@@ -785,7 +785,7 @@ namespace mongo {
switch(e.type()){
case Array:{
BSONObjBuilder subb;
- appendArray(subb , e.embeddedObject());
+ appendArray(subb , e.embeddedObject(), true);
b.appendArray(b.numStr(i++).c_str(), subb.obj());
break;
}
View
2  db/queryutil.h
@@ -214,7 +214,7 @@ namespace mongo {
void add( const string& field, bool include );
void add( const string& field, int skip, int limit );
- void appendArray( BSONObjBuilder& b , const BSONObj& a ) const;
+ void appendArray( BSONObjBuilder& b , const BSONObj& a , bool nested=false) const;
bool _include; // true if default at this level is to include
bool _special; // true if this level can't be skipped or included without recursing
View
14 jstests/slice1.js
@@ -52,3 +52,17 @@ assert.eq(out.title , undefined);
out = t.findOne({}, {comments:{$slice:2}, 'comments.id':false});
assert.eq(out.comments , [{text: 'a'}, {text: 'b'}]);
assert.eq(out.title , 'foo');
+
+//nested arrays
+t.drop();
+t.insert({_id:1, a:[[1,1,1], [2,2,2], [3,3,3]], b:1, c:1});
+
+out = t.findOne({}, {a:{$slice:1}});
+assert.eq(out.a , [[1,1,1]], 'n 1');
+
+out = t.findOne({}, {a:{$slice:-1}});
+assert.eq(out.a , [[3,3,3]], 'n 2');
+
+out = t.findOne({}, {a:{$slice:[0,2]}});
+assert.eq(out.a , [[1,1,1],[2,2,2]], 'n 2');
+
Please sign in to comment.
Something went wrong with that request. Please try again.