diff options
author | David Storch <david.storch@10gen.com> | 2017-05-04 16:43:38 -0400 |
---|---|---|
committer | David Storch <david.storch@10gen.com> | 2017-05-08 09:46:23 -0400 |
commit | c6d0c0dd01c328d8ed1a5744485b29826b1fb2c9 (patch) | |
tree | 7bfab00981c8a5a0b50102cdbe3dab1ebe17234d /jstests | |
parent | 2c18910e75b78ed228ea5f600f51ca4fa6f1aaf6 (diff) | |
download | mongo-c6d0c0dd01c328d8ed1a5744485b29826b1fb2c9.tar.gz |
SERVER-1475 Make $type:'array' match outer arrays.
Prior to this change, the semantics of $type were that it
only matches documents with nested arrays, e.g. {x: {$type:
'array'}} would match {x: [1, [2, 3]]} but not {x: [1, 2,
3]}. This is inconsistent with the matching semantics for
other query predicate operators, which apply both to the
individual array elements and the array as a whole.
The new behavior is that single-level arrays as well as
nested arrays will match {$type:'array'}. This is a breaking
change for applications which rely on the old behavior.
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/type_array.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/jstests/core/type_array.js b/jstests/core/type_array.js new file mode 100644 index 00000000000..1ac1fd4736a --- /dev/null +++ b/jstests/core/type_array.js @@ -0,0 +1,41 @@ +/** + * Tests for the array-related behavior of the $type query operator. + */ +(function() { + "use strict"; + + let coll = db.jstest_type_array; + coll.drop(); + + /** + * Iterates 'cursor' and returns a sorted array of the '_id' fields for the returned documents. + */ + function extractSortedIdsFromCursor(cursor) { + let ids = []; + while (cursor.hasNext()) { + ids.push(cursor.next()._id); + } + return ids.sort(); + } + + function runTests() { + assert.eq([1, 2, 6], extractSortedIdsFromCursor(coll.find({a: {$type: "number"}}))); + assert.eq([2, 7], extractSortedIdsFromCursor(coll.find({a: {$type: "string"}}))); + assert.eq([1, 2, 3, 4, 5], extractSortedIdsFromCursor(coll.find({a: {$type: "array"}}))); + assert.eq([4, 5], extractSortedIdsFromCursor(coll.find({"a.0": {$type: "array"}}))); + assert.eq([5], extractSortedIdsFromCursor(coll.find({"a.0.0": {$type: "array"}}))); + } + + assert.writeOK(coll.insert({_id: 1, a: [1, 2, 3]})); + assert.writeOK(coll.insert({_id: 2, a: [1, "foo", 3]})); + assert.writeOK(coll.insert({_id: 3, a: []})); + assert.writeOK(coll.insert({_id: 4, a: [[]]})); + assert.writeOK(coll.insert({_id: 5, a: [[[]]]})); + assert.writeOK(coll.insert({_id: 6, a: 1})); + assert.writeOK(coll.insert({_id: 7, a: "foo"})); + + // Verify $type queries both with and without an index. + runTests(); + assert.writeOK(coll.createIndex({a: 1})); + runTests(); +}()); |