summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/is_number.js
blob: ddf75dba4dcbdf3a4cf6d0a600b5d740b85d84ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * Tests for the $isNumber aggregation expression.
 * @tags: [
 *   sbe_incompatible,
 * ]
 */
(function() {
'use strict';
const coll = db.isNumber_expr;
coll.drop();

function testIsNumber(inputExprPath, expectedOutput, inputId) {
    const result = coll.aggregate([
                           {"$match": {_id: inputId}},
                           {"$project": {_id: 0, "isNum": {"$isNumber": inputExprPath}}},
                       ])
                       .toArray();
    assert.eq(result, expectedOutput);
}

// Test when $isNumber evaluates to an integer, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 1, integerFieldPath: NumberInt(56072)}));
testIsNumber("$integerFieldPath", [{"isNum": true}], 1);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 11, nestedPath: {nestedNumber: NumberInt(500)}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 11);

// Test when $isNumber evaluates to a double, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 2, doubleFieldPath: 56072.2}));
testIsNumber("$doubleFieldPath", [{"isNum": true}], 2);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 12, nestedPath: {nestedNumber: 56072.2}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 12);

// Test when $isNumber evaluates to a decimal, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 3, decimalFieldPath: NumberDecimal("1.234")}));
testIsNumber("$decimalFieldPath", [{"isNum": true}], 3);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 13, nestedPath: {nestedNumber: NumberDecimal("1.234")}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 13);

// Test when $isNumber evaluates to a long when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 4, longFieldPath: NumberLong("123456789")}));
testIsNumber("$longFieldPath", [{"isNum": true}], 4);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 14, nestedPath: {nestedNumber: NumberLong("123456789")}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 14);

// Test when $isNumber evaluates to null, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 5, nullFieldPath: null}));
testIsNumber("$nullFieldPath", [{"isNum": false}], 5);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 15, nestedPath: {nestedNull: null}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 15);

// Test when $isNumber evaluates to missing, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 6}));
testIsNumber("$missingFieldPath", [{"isNum": false}], 6);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 16, nestedPath: {}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 16);

// Test when $isNumber evaluates to an array, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 7, arrayFieldPath: [1]}));
testIsNumber("$arrayFieldPath", [{"isNum": false}], 7);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 17, nestedPath: {nestedArray: [1]}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 17);

// Test when $isNumber evaluates to a string, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 8, stringFieldPath: "1234"}));
testIsNumber("$stringFieldPath", [{"isNum": false}], 8);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 18, nestedPath: {nestedString: "12345"}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 18);

// Test when $isNumber evaluates to a Date, when the input expression is a document field.
assert.commandWorked(coll.insert({_id: 9, dateFieldPath: new Date()}));
testIsNumber("$dateFieldPath", [{"isNum": false}], 9);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 19, nestedPath: {nestedDate: new Date()}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 19);

// Test when $isNumber evaluates to a BinData, when the input expression is a document field.
// (UUID's are encoded as binary data)
assert.commandWorked(coll.insert({_id: 10, binDataFieldPath: UUID()}));
testIsNumber("$binDataFieldPath", [{"isNum": false}], 10);
// Same as above, but when the input expression is a nested document field.
assert.commandWorked(coll.insert({_id: 20, nestedPath: {nestedBinData: UUID()}}));
testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 20);

// Test a few literal expressions, rather than ones retrieved from a document.
assert.commandWorked(coll.insert({_id: 21}));

// Test when $isNumber's input expression is a literal long.
testIsNumber(NumberLong("12345678"), [{"isNum": true}], 21);

// Test when $isNumber's input expression is a literal decimal.
testIsNumber(NumberDecimal("1.2345678"), [{"isNum": true}], 21);

// Test when $isNumber's input expression is a literal int.
testIsNumber(NumberInt(18), [{"isNum": true}], 21);

// Test when $isNumber's input expression is a literal double.
testIsNumber(5.34, [{"isNum": true}], 21);

// Test when $isNumber's input expression is null literal.
testIsNumber(null, [{"isNum": false}], 21);

// Test when $isNumber's input expression is a literal array.
testIsNumber([[2]], [{"isNum": false}], 21);

// Test when $isNumber's input expression is a literal string.
testIsNumber("a literal string", [{"isNum": false}], 21);

// Test when $isNumber's input expression is a literal Date.
testIsNumber(new Date(), [{"isNum": false}], 21);

// Test when $isNumber's input expression is a literal BinData.
testIsNumber(UUID(), [{"isNum": false}], 21);
}());