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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kQuery
#include "mongo/platform/basic.h"
#include "mongo/db/matcher/schema/json_schema_parser.h"
#include <memory>
#include "mongo/bson/bsontypes.h"
#include "mongo/bson/unordered_fields_bsonelement_comparator.h"
#include "mongo/db/commands/feature_compatibility_version_documentation.h"
#include "mongo/db/matcher/expression_always_boolean.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/matcher/matcher_type_set.h"
#include "mongo/db/matcher/schema/encrypt_schema_gen.h"
#include "mongo/db/matcher/schema/expression_internal_schema_all_elem_match_from_index.h"
#include "mongo/db/matcher/schema/expression_internal_schema_allowed_properties.h"
#include "mongo/db/matcher/schema/expression_internal_schema_cond.h"
#include "mongo/db/matcher/schema/expression_internal_schema_eq.h"
#include "mongo/db/matcher/schema/expression_internal_schema_fmod.h"
#include "mongo/db/matcher/schema/expression_internal_schema_match_array_index.h"
#include "mongo/db/matcher/schema/expression_internal_schema_max_items.h"
#include "mongo/db/matcher/schema/expression_internal_schema_max_length.h"
#include "mongo/db/matcher/schema/expression_internal_schema_max_properties.h"
#include "mongo/db/matcher/schema/expression_internal_schema_min_items.h"
#include "mongo/db/matcher/schema/expression_internal_schema_min_length.h"
#include "mongo/db/matcher/schema/expression_internal_schema_min_properties.h"
#include "mongo/db/matcher/schema/expression_internal_schema_object_match.h"
#include "mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.h"
#include "mongo/db/matcher/schema/expression_internal_schema_unique_items.h"
#include "mongo/db/matcher/schema/expression_internal_schema_xor.h"
#include "mongo/db/matcher/schema/json_pointer.h"
#include "mongo/logger/log_component_settings.h"
#include "mongo/logv2/log.h"
#include "mongo/util/log.h"
#include "mongo/util/string_map.h"
namespace mongo {
using PatternSchema = InternalSchemaAllowedPropertiesMatchExpression::PatternSchema;
using Pattern = InternalSchemaAllowedPropertiesMatchExpression::Pattern;
namespace {
using findBSONTypeAliasFun = std::function<boost::optional<BSONType>(StringData)>;
// Explicitly unsupported JSON Schema keywords.
const std::set<StringData> unsupportedKeywords{
"$ref"_sd,
"$schema"_sd,
"default"_sd,
"definitions"_sd,
"format"_sd,
"id"_sd,
};
constexpr StringData kNamePlaceholder = "i"_sd;
/**
* Parses 'schema' to the semantically equivalent match expression. If the schema has an associated
* path, e.g. if we are parsing the nested schema for property "myProp" in
*
* {properties: {myProp: <nested-schema>}}
*
* then this is passed in 'path'. In this example, the value of 'path' is "myProp". If there is no
* path, e.g. for top-level schemas, then 'path' is empty.
*/
StatusWithMatchExpression _parse(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONObj schema,
bool ignoreUnknownKeywords);
/**
* Constructs and returns a match expression to evaluate a JSON Schema restriction keyword.
*
* This handles semantic differences between the MongoDB query language and JSON Schema. MongoDB
* match expressions which apply to a particular type will reject non-matching types, whereas JSON
* Schema restriction keywords allow non-matching types. As an example, consider the maxItems
* keyword. This keyword only applies in JSON Schema if the type is an array, whereas the
* $_internalSchemaMaxItems match expression node rejects non-arrays.
*
* The 'restrictionType' expresses the type to which the JSON Schema restriction applies (e.g.
* arrays for maxItems). The 'restrictionExpr' is the match expression node which can be used to
* enforce this restriction, should the types match (e.g. $_internalSchemaMaxItems). 'statedType' is
* a parsed representation of the JSON Schema type keyword which is in effect.
*/
std::unique_ptr<MatchExpression> makeRestriction(const MatcherTypeSet& restrictionType,
StringData path,
std::unique_ptr<MatchExpression> restrictionExpr,
InternalSchemaTypeExpression* statedType) {
invariant(restrictionType.isSingleType());
if (statedType && statedType->typeSet().isSingleType()) {
// Use NumberInt in the "number" case as a stand-in.
BSONType statedBSONType = statedType->typeSet().allNumbers
? BSONType::NumberInt
: *statedType->typeSet().bsonTypes.begin();
if (restrictionType.hasType(statedBSONType)) {
// This restriction applies to the type that is already being enforced. We return the
// restriction unmodified.
return restrictionExpr;
} else {
// This restriction doesn't take any effect, since the type of the schema is different
// from the type to which this retriction applies.
return std::make_unique<AlwaysTrueMatchExpression>();
}
}
// Generate and return the following expression tree:
//
// (OR (<restrictionExpr>) (NOT (INTERNAL_SCHEMA_TYPE <restrictionType>))
//
// We need to do this because restriction keywords do not apply when a field is either not
// present or of a different type.
auto typeExpr = std::make_unique<InternalSchemaTypeExpression>(path, restrictionType);
auto notExpr = std::make_unique<NotMatchExpression>(typeExpr.release());
auto orExpr = std::make_unique<OrMatchExpression>();
orExpr->add(notExpr.release());
orExpr->add(restrictionExpr.release());
return std::move(orExpr);
}
StatusWith<std::unique_ptr<InternalSchemaTypeExpression>> parseType(
StringData path,
StringData keywordName,
BSONElement typeElt,
const findBSONTypeAliasFun& aliasMapFind) {
auto typeSet = JSONSchemaParser::parseTypeSet(typeElt, aliasMapFind);
if (!typeSet.isOK()) {
return typeSet.getStatus();
}
if (typeSet.getValue().isEmpty()) {
return {Status(ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << keywordName
<< "' must name at least one type")};
}
auto typeExpr =
std::make_unique<InternalSchemaTypeExpression>(path, std::move(typeSet.getValue()));
return {std::move(typeExpr)};
}
StatusWithMatchExpression parseMaximum(StringData path,
BSONElement maximum,
InternalSchemaTypeExpression* typeExpr,
bool isExclusiveMaximum) {
if (!maximum.isNumber()) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMaximumKeyword
<< "' must be a number")};
}
if (path.empty()) {
// This restriction has no effect in a top-level schema, since we only store objects.
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
std::unique_ptr<ComparisonMatchExpression> expr;
if (isExclusiveMaximum) {
expr = std::make_unique<LTMatchExpression>(path, maximum);
} else {
expr = std::make_unique<LTEMatchExpression>(path, maximum);
}
MatcherTypeSet restrictionType;
restrictionType.allNumbers = true;
return makeRestriction(restrictionType, path, std::move(expr), typeExpr);
}
StatusWithMatchExpression parseMinimum(StringData path,
BSONElement minimum,
InternalSchemaTypeExpression* typeExpr,
bool isExclusiveMinimum) {
if (!minimum.isNumber()) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMinimumKeyword
<< "' must be a number")};
}
if (path.empty()) {
// This restriction has no effect in a top-level schema, since we only store objects.
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
std::unique_ptr<ComparisonMatchExpression> expr;
if (isExclusiveMinimum) {
expr = std::make_unique<GTMatchExpression>(path, minimum);
} else {
expr = std::make_unique<GTEMatchExpression>(path, minimum);
}
MatcherTypeSet restrictionType;
restrictionType.allNumbers = true;
return makeRestriction(restrictionType, path, std::move(expr), typeExpr);
}
/**
* Parses length-related keywords that expect a nonnegative long as an argument.
*/
template <class T>
StatusWithMatchExpression parseLength(StringData path,
BSONElement length,
InternalSchemaTypeExpression* typeExpr,
BSONType restrictionType) {
auto parsedLength = length.parseIntegerElementToNonNegativeLong();
if (!parsedLength.isOK()) {
return parsedLength.getStatus();
}
if (path.empty()) {
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
auto expr = std::make_unique<T>(path, parsedLength.getValue());
return makeRestriction(restrictionType, path, std::move(expr), typeExpr);
}
StatusWithMatchExpression parsePattern(StringData path,
BSONElement pattern,
InternalSchemaTypeExpression* typeExpr) {
if (pattern.type() != BSONType::String) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaPatternKeyword
<< "' must be a string")};
}
if (path.empty()) {
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
// JSON Schema does not allow regex flags to be specified.
constexpr auto emptyFlags = "";
auto expr = std::make_unique<RegexMatchExpression>(path, pattern.valueStringData(), emptyFlags);
return makeRestriction(BSONType::String, path, std::move(expr), typeExpr);
}
StatusWithMatchExpression parseMultipleOf(StringData path,
BSONElement multipleOf,
InternalSchemaTypeExpression* typeExpr) {
if (!multipleOf.isNumber()) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMultipleOfKeyword
<< "' must be a number")};
}
if (multipleOf.numberDecimal().isNegative() || multipleOf.numberDecimal().isZero()) {
return {Status(ErrorCodes::FailedToParse,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMultipleOfKeyword
<< "' must have a positive value")};
}
if (path.empty()) {
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
auto expr = std::make_unique<InternalSchemaFmodMatchExpression>(
path, multipleOf.numberDecimal(), Decimal128(0));
MatcherTypeSet restrictionType;
restrictionType.allNumbers = true;
return makeRestriction(restrictionType, path, std::move(expr), typeExpr);
}
template <class T>
StatusWithMatchExpression parseLogicalKeyword(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement logicalElement,
bool ignoreUnknownKeywords) {
if (logicalElement.type() != BSONType::Array) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << logicalElement.fieldNameStringData()
<< "' must be an array"};
}
auto logicalElementObj = logicalElement.embeddedObject();
if (logicalElementObj.isEmpty()) {
return {ErrorCodes::BadValue,
str::stream() << "$jsonSchema keyword '" << logicalElement.fieldNameStringData()
<< "' must be a non-empty array"};
}
std::unique_ptr<T> listOfExpr = std::make_unique<T>();
for (const auto& elem : logicalElementObj) {
if (elem.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << logicalElement.fieldNameStringData()
<< "' must be an array of objects, but found an element of type "
<< elem.type()};
}
auto nestedSchemaMatch = _parse(expCtx, path, elem.embeddedObject(), ignoreUnknownKeywords);
if (!nestedSchemaMatch.isOK()) {
return nestedSchemaMatch.getStatus();
}
listOfExpr->add(nestedSchemaMatch.getValue().release());
}
return {std::move(listOfExpr)};
}
StatusWithMatchExpression parseEnum(StringData path, BSONElement enumElement) {
if (enumElement.type() != BSONType::Array) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaEnumKeyword
<< "' must be an array, but found an element of type "
<< enumElement.type()};
}
auto enumArray = enumElement.embeddedObject();
if (enumArray.isEmpty()) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaEnumKeyword
<< "' cannot be an empty array"};
}
auto orExpr = std::make_unique<OrMatchExpression>();
UnorderedFieldsBSONElementComparator eltComp;
BSONEltSet eqSet = eltComp.makeBSONEltSet();
for (auto&& arrayElem : enumArray) {
auto insertStatus = eqSet.insert(arrayElem);
if (!insertStatus.second) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaEnumKeyword
<< "' array cannot contain duplicate values."};
}
// 'enum' at the top-level implies a literal object match on the root document.
if (path.empty()) {
// Top-level non-object enum values can be safely ignored, since MongoDB only stores
// objects, not scalars or arrays.
if (arrayElem.type() == BSONType::Object) {
auto rootDocEq = std::make_unique<InternalSchemaRootDocEqMatchExpression>(
arrayElem.embeddedObject());
orExpr->add(rootDocEq.release());
}
} else {
auto eqExpr = std::make_unique<InternalSchemaEqMatchExpression>(path, arrayElem);
orExpr->add(eqExpr.release());
}
}
// Make sure that the OR expression has at least 1 child.
if (orExpr->numChildren() == 0) {
return {std::make_unique<AlwaysFalseMatchExpression>()};
}
return {std::move(orExpr)};
}
/**
* Given a BSON element corresponding to the $jsonSchema "required" keyword, returns the set of
* required property names. If the contents of the "required" keyword are invalid, returns a non-OK
* status.
*/
StatusWith<StringDataSet> parseRequired(BSONElement requiredElt) {
if (requiredElt.type() != BSONType::Array) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaRequiredKeyword
<< "' must be an array, but found an element of type "
<< requiredElt.type()};
}
StringDataSet properties;
for (auto&& propertyName : requiredElt.embeddedObject()) {
if (propertyName.type() != BSONType::String) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaRequiredKeyword
<< "' must be an array of strings, but found an element of type: "
<< propertyName.type()};
}
const auto [it, didInsert] = properties.insert(propertyName.valueStringData());
if (!didInsert) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaRequiredKeyword
<< "' array cannot contain duplicate values"};
}
}
if (properties.empty()) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaRequiredKeyword
<< "' cannot be an empty array"};
}
return std::move(properties);
}
/**
* Given the already-parsed set of required properties, returns a MatchExpression which ensures that
* those properties exist. Returns a parsing error if the translation fails.
*/
StatusWithMatchExpression translateRequired(const StringDataSet& requiredProperties,
StringData path,
InternalSchemaTypeExpression* typeExpr) {
auto andExpr = std::make_unique<AndMatchExpression>();
std::vector<StringData> sortedProperties(requiredProperties.begin(), requiredProperties.end());
std::sort(sortedProperties.begin(), sortedProperties.end());
for (auto&& propertyName : sortedProperties) {
andExpr->add(new ExistsMatchExpression(propertyName));
}
// If this is a top-level schema, then we know that we are matching against objects, and there
// is no need to worry about ensuring that non-objects match.
if (path.empty()) {
return {std::move(andExpr)};
}
auto objectMatch =
std::make_unique<InternalSchemaObjectMatchExpression>(path, std::move(andExpr));
return makeRestriction(BSONType::Object, path, std::move(objectMatch), typeExpr);
}
StatusWithMatchExpression parseProperties(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement propertiesElt,
InternalSchemaTypeExpression* typeExpr,
const StringDataSet& requiredProperties,
bool ignoreUnknownKeywords) {
if (propertiesElt.type() != BSONType::Object) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaPropertiesKeyword
<< "' must be an object")};
}
auto propertiesObj = propertiesElt.embeddedObject();
auto andExpr = std::make_unique<AndMatchExpression>();
for (auto&& property : propertiesObj) {
if (property.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "Nested schema for $jsonSchema property '"
<< property.fieldNameStringData() << "' must be an object"};
}
auto nestedSchemaMatch = _parse(expCtx,
property.fieldNameStringData(),
property.embeddedObject(),
ignoreUnknownKeywords);
if (!nestedSchemaMatch.isOK()) {
return nestedSchemaMatch.getStatus();
}
if (requiredProperties.find(property.fieldNameStringData()) != requiredProperties.end()) {
// The field name for which we created the nested schema is a required property. This
// property must exist and therefore must match 'nestedSchemaMatch'.
andExpr->add(nestedSchemaMatch.getValue().release());
} else {
// This property either must not exist or must match the nested schema. Therefore, we
// generate the match expression (OR (NOT (EXISTS)) <nestedSchemaMatch>).
auto existsExpr =
std::make_unique<ExistsMatchExpression>(property.fieldNameStringData());
auto notExpr = std::make_unique<NotMatchExpression>(existsExpr.release());
auto orExpr = std::make_unique<OrMatchExpression>();
orExpr->add(notExpr.release());
orExpr->add(nestedSchemaMatch.getValue().release());
andExpr->add(orExpr.release());
}
}
// If this is a top-level schema, then we have no path and there is no need for an
// explicit object match node.
if (path.empty()) {
return {std::move(andExpr)};
}
auto objectMatch =
std::make_unique<InternalSchemaObjectMatchExpression>(path, std::move(andExpr));
return makeRestriction(BSONType::Object, path, std::move(objectMatch), typeExpr);
}
StatusWith<std::vector<PatternSchema>> parsePatternProperties(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
BSONElement patternPropertiesElt,
bool ignoreUnknownKeywords) {
std::vector<PatternSchema> patternProperties;
if (!patternPropertiesElt) {
return {std::move(patternProperties)};
}
if (patternPropertiesElt.type() != BSONType::Object) {
return {Status(ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaPatternPropertiesKeyword
<< "' must be an object")};
}
for (auto&& patternSchema : patternPropertiesElt.embeddedObject()) {
if (patternSchema.type() != BSONType::Object) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaPatternPropertiesKeyword
<< "' has property '" << patternSchema.fieldNameStringData()
<< "' which is not an object")};
}
// Parse the nested schema using a placeholder as the path, since we intend on using the
// resulting match expression inside an ExpressionWithPlaceholder.
auto nestedSchemaMatch =
_parse(expCtx, kNamePlaceholder, patternSchema.embeddedObject(), ignoreUnknownKeywords);
if (!nestedSchemaMatch.isOK()) {
return nestedSchemaMatch.getStatus();
}
auto exprWithPlaceholder = std::make_unique<ExpressionWithPlaceholder>(
kNamePlaceholder.toString(), std::move(nestedSchemaMatch.getValue()));
Pattern pattern{patternSchema.fieldNameStringData()};
patternProperties.emplace_back(std::move(pattern), std::move(exprWithPlaceholder));
}
return {std::move(patternProperties)};
}
StatusWithMatchExpression parseAdditionalProperties(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
BSONElement additionalPropertiesElt,
bool ignoreUnknownKeywords) {
if (!additionalPropertiesElt) {
// The absence of the 'additionalProperties' keyword is identical in meaning to the presence
// of 'additionalProperties' with a value of true.
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
if (additionalPropertiesElt.type() != BSONType::Bool &&
additionalPropertiesElt.type() != BSONType::Object) {
return {Status(ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaAdditionalPropertiesKeyword
<< "' must be an object or a boolean")};
}
if (additionalPropertiesElt.type() == BSONType::Bool) {
if (additionalPropertiesElt.boolean()) {
return {std::make_unique<AlwaysTrueMatchExpression>()};
} else {
return {std::make_unique<AlwaysFalseMatchExpression>()};
}
}
// Parse the nested schema using a placeholder as the path, since we intend on using the
// resulting match expression inside an ExpressionWithPlaceholder.
auto nestedSchemaMatch = _parse(
expCtx, kNamePlaceholder, additionalPropertiesElt.embeddedObject(), ignoreUnknownKeywords);
if (!nestedSchemaMatch.isOK()) {
return nestedSchemaMatch.getStatus();
}
return {std::move(nestedSchemaMatch.getValue())};
}
/**
* Returns a match expression which handles both the 'additionalProperties' and 'patternProperties'
* keywords.
*/
StatusWithMatchExpression parseAllowedProperties(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement propertiesElt,
BSONElement patternPropertiesElt,
BSONElement additionalPropertiesElt,
InternalSchemaTypeExpression* typeExpr,
bool ignoreUnknownKeywords) {
// Collect the set of properties named by the 'properties' keyword.
StringDataSet propertyNames;
if (propertiesElt) {
std::vector<StringData> propertyNamesVec;
for (auto&& elem : propertiesElt.embeddedObject()) {
propertyNamesVec.push_back(elem.fieldNameStringData());
}
propertyNames.insert(propertyNamesVec.begin(), propertyNamesVec.end());
}
auto patternProperties =
parsePatternProperties(expCtx, patternPropertiesElt, ignoreUnknownKeywords);
if (!patternProperties.isOK()) {
return patternProperties.getStatus();
}
auto otherwiseExpr =
parseAdditionalProperties(expCtx, additionalPropertiesElt, ignoreUnknownKeywords);
if (!otherwiseExpr.isOK()) {
return otherwiseExpr.getStatus();
}
auto otherwiseWithPlaceholder = std::make_unique<ExpressionWithPlaceholder>(
kNamePlaceholder.toString(), std::move(otherwiseExpr.getValue()));
auto allowedPropertiesExpr = std::make_unique<InternalSchemaAllowedPropertiesMatchExpression>(
std::move(propertyNames),
kNamePlaceholder,
std::move(patternProperties.getValue()),
std::move(otherwiseWithPlaceholder));
// If this is a top-level schema, then we have no path and there is no need for an explicit
// object match node.
if (path.empty()) {
return {std::move(allowedPropertiesExpr)};
}
auto objectMatch = std::make_unique<InternalSchemaObjectMatchExpression>(
path, std::move(allowedPropertiesExpr));
return makeRestriction(BSONType::Object, path, std::move(objectMatch), typeExpr);
}
/**
* Parses 'minProperties' and 'maxProperties' JSON Schema keywords.
*/
template <class T>
StatusWithMatchExpression parseNumProperties(StringData path,
BSONElement numProperties,
InternalSchemaTypeExpression* typeExpr) {
auto parsedNumProps = numProperties.parseIntegerElementToNonNegativeLong();
if (!parsedNumProps.isOK()) {
return parsedNumProps.getStatus();
}
auto expr = std::make_unique<T>(parsedNumProps.getValue());
if (path.empty()) {
// This is a top-level schema.
return {std::move(expr)};
}
auto objectMatch = std::make_unique<InternalSchemaObjectMatchExpression>(path, std::move(expr));
return makeRestriction(BSONType::Object, path, std::move(objectMatch), typeExpr);
}
StatusWithMatchExpression makeDependencyExistsClause(StringData path, StringData dependencyName) {
auto existsExpr = std::make_unique<ExistsMatchExpression>(dependencyName);
if (path.empty()) {
return {std::move(existsExpr)};
}
auto objectMatch =
std::make_unique<InternalSchemaObjectMatchExpression>(path, std::move(existsExpr));
return {std::move(objectMatch)};
}
StatusWithMatchExpression translateSchemaDependency(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement dependency,
bool ignoreUnknownKeywords) {
invariant(dependency.type() == BSONType::Object);
auto nestedSchemaMatch =
_parse(expCtx, path, dependency.embeddedObject(), ignoreUnknownKeywords);
if (!nestedSchemaMatch.isOK()) {
return nestedSchemaMatch.getStatus();
}
auto ifClause = makeDependencyExistsClause(path, dependency.fieldNameStringData());
if (!ifClause.isOK()) {
return ifClause.getStatus();
}
std::array<std::unique_ptr<MatchExpression>, 3> expressions = {
std::move(ifClause.getValue()),
std::move(nestedSchemaMatch.getValue()),
std::make_unique<AlwaysTrueMatchExpression>()};
auto condExpr = std::make_unique<InternalSchemaCondMatchExpression>(std::move(expressions));
return {std::move(condExpr)};
}
StatusWithMatchExpression translatePropertyDependency(StringData path, BSONElement dependency) {
invariant(dependency.type() == BSONType::Array);
if (dependency.embeddedObject().isEmpty()) {
return {ErrorCodes::FailedToParse,
str::stream() << "property '" << dependency.fieldNameStringData()
<< "' in $jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDependenciesKeyword
<< "' cannot be an empty array"};
}
auto propertyDependencyExpr = std::make_unique<AndMatchExpression>();
std::set<StringData> propertyDependencyNames;
for (auto&& propertyDependency : dependency.embeddedObject()) {
if (propertyDependency.type() != BSONType::String) {
return {ErrorCodes::TypeMismatch,
str::stream() << "array '" << dependency.fieldNameStringData()
<< "' in $jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDependenciesKeyword
<< "' can only contain strings, but found element of type: "
<< typeName(propertyDependency.type())};
}
auto insertionResult = propertyDependencyNames.insert(propertyDependency.valueStringData());
if (!insertionResult.second) {
return {ErrorCodes::FailedToParse,
str::stream() << "array '" << dependency.fieldNameStringData()
<< "' in $jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDependenciesKeyword
<< "' contains duplicate element: "
<< propertyDependency.valueStringData()};
}
auto propertyExistsExpr =
makeDependencyExistsClause(path, propertyDependency.valueStringData());
if (!propertyExistsExpr.isOK()) {
return propertyExistsExpr.getStatus();
}
propertyDependencyExpr->add(propertyExistsExpr.getValue().release());
}
auto ifClause = makeDependencyExistsClause(path, dependency.fieldNameStringData());
if (!ifClause.isOK()) {
return ifClause.getStatus();
}
std::array<std::unique_ptr<MatchExpression>, 3> expressions = {
{std::move(ifClause.getValue()),
std::move(propertyDependencyExpr),
std::make_unique<AlwaysTrueMatchExpression>()}};
auto condExpr = std::make_unique<InternalSchemaCondMatchExpression>(std::move(expressions));
return {std::move(condExpr)};
}
StatusWithMatchExpression parseDependencies(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement dependencies,
bool ignoreUnknownKeywords) {
if (dependencies.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDependenciesKeyword
<< "' must be an object"};
}
auto andExpr = std::make_unique<AndMatchExpression>();
for (auto&& dependency : dependencies.embeddedObject()) {
if (dependency.type() != BSONType::Object && dependency.type() != BSONType::Array) {
return {ErrorCodes::TypeMismatch,
str::stream() << "property '" << dependency.fieldNameStringData()
<< "' in $jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDependenciesKeyword
<< "' must be either an object or an array"};
}
auto dependencyExpr = (dependency.type() == BSONType::Object)
? translateSchemaDependency(expCtx, path, dependency, ignoreUnknownKeywords)
: translatePropertyDependency(path, dependency);
if (!dependencyExpr.isOK()) {
return dependencyExpr.getStatus();
}
andExpr->add(dependencyExpr.getValue().release());
}
return {std::move(andExpr)};
}
StatusWithMatchExpression parseUniqueItems(BSONElement uniqueItemsElt,
StringData path,
InternalSchemaTypeExpression* typeExpr) {
if (!uniqueItemsElt.isBoolean()) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaUniqueItemsKeyword
<< "' must be a boolean"};
} else if (path.empty()) {
return {std::make_unique<AlwaysTrueMatchExpression>()};
} else if (uniqueItemsElt.boolean()) {
auto uniqueItemsExpr = std::make_unique<InternalSchemaUniqueItemsMatchExpression>(path);
return makeRestriction(BSONType::Array, path, std::move(uniqueItemsExpr), typeExpr);
}
return {std::make_unique<AlwaysTrueMatchExpression>()};
}
/**
* Parses 'itemsElt' into a match expression and adds it to 'andExpr'. On success, returns the index
* from which the "additionalItems" schema should be enforced, if needed.
*/
StatusWith<boost::optional<long long>> parseItems(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement itemsElt,
bool ignoreUnknownKeywords,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr) {
boost::optional<long long> startIndexForAdditionalItems;
if (itemsElt.type() == BSONType::Array) {
// When "items" is an array, generate match expressions for each subschema for each position
// in the array, which are bundled together in an AndMatchExpression.
auto andExprForSubschemas = std::make_unique<AndMatchExpression>();
auto index = 0LL;
for (auto subschema : itemsElt.embeddedObject()) {
if (subschema.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaItemsKeyword
<< "' requires that each element of the array is an "
"object, but found a "
<< subschema.type()};
}
// We want to make an ExpressionWithPlaceholder for $_internalSchemaMatchArrayIndex,
// so we use our default placeholder as the path.
auto parsedSubschema =
_parse(expCtx, kNamePlaceholder, subschema.embeddedObject(), ignoreUnknownKeywords);
if (!parsedSubschema.isOK()) {
return parsedSubschema.getStatus();
}
auto exprWithPlaceholder = std::make_unique<ExpressionWithPlaceholder>(
kNamePlaceholder.toString(), std::move(parsedSubschema.getValue()));
auto matchArrayIndex = std::make_unique<InternalSchemaMatchArrayIndexMatchExpression>(
path, index, std::move(exprWithPlaceholder));
andExprForSubschemas->add(matchArrayIndex.release());
++index;
}
startIndexForAdditionalItems = index;
if (path.empty()) {
andExpr->add(std::make_unique<AlwaysTrueMatchExpression>().release());
} else {
andExpr->add(
makeRestriction(BSONType::Array, path, std::move(andExprForSubschemas), typeExpr)
.release());
}
} else if (itemsElt.type() == BSONType::Object) {
// When "items" is an object, generate a single AllElemMatchFromIndex that applies to every
// element in the array to match. The parsed expression is intended for an
// ExpressionWithPlaceholder, so we use the default placeholder as the path.
auto nestedItemsSchema =
_parse(expCtx, kNamePlaceholder, itemsElt.embeddedObject(), ignoreUnknownKeywords);
if (!nestedItemsSchema.isOK()) {
return nestedItemsSchema.getStatus();
}
auto exprWithPlaceholder = std::make_unique<ExpressionWithPlaceholder>(
kNamePlaceholder.toString(), std::move(nestedItemsSchema.getValue()));
if (path.empty()) {
andExpr->add(std::make_unique<AlwaysTrueMatchExpression>().release());
} else {
constexpr auto startIndexForItems = 0LL;
auto allElemMatch =
std::make_unique<InternalSchemaAllElemMatchFromIndexMatchExpression>(
path, startIndexForItems, std::move(exprWithPlaceholder));
andExpr->add(makeRestriction(BSONType::Array, path, std::move(allElemMatch), typeExpr)
.release());
}
} else {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaItemsKeyword
<< "' must be an array or an object, not " << itemsElt.type()};
}
return startIndexForAdditionalItems;
}
Status parseAdditionalItems(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONElement additionalItemsElt,
boost::optional<long long> startIndexForAdditionalItems,
bool ignoreUnknownKeywords,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr) {
std::unique_ptr<ExpressionWithPlaceholder> otherwiseExpr;
if (additionalItemsElt.type() == BSONType::Bool) {
const auto emptyPlaceholder = boost::none;
if (additionalItemsElt.boolean()) {
otherwiseExpr = std::make_unique<ExpressionWithPlaceholder>(
emptyPlaceholder, std::make_unique<AlwaysTrueMatchExpression>());
} else {
otherwiseExpr = std::make_unique<ExpressionWithPlaceholder>(
emptyPlaceholder, std::make_unique<AlwaysFalseMatchExpression>());
}
} else if (additionalItemsElt.type() == BSONType::Object) {
auto parsedOtherwiseExpr = _parse(
expCtx, kNamePlaceholder, additionalItemsElt.embeddedObject(), ignoreUnknownKeywords);
if (!parsedOtherwiseExpr.isOK()) {
return parsedOtherwiseExpr.getStatus();
}
otherwiseExpr = std::make_unique<ExpressionWithPlaceholder>(
kNamePlaceholder.toString(), std::move(parsedOtherwiseExpr.getValue()));
} else {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaAdditionalItemsKeyword
<< "' must be either an object or a boolean, but got a "
<< additionalItemsElt.type()};
}
// Only generate a match expression if needed.
if (startIndexForAdditionalItems) {
if (path.empty()) {
andExpr->add(std::make_unique<AlwaysTrueMatchExpression>().release());
} else {
auto allElemMatch =
std::make_unique<InternalSchemaAllElemMatchFromIndexMatchExpression>(
path, *startIndexForAdditionalItems, std::move(otherwiseExpr));
andExpr->add(makeRestriction(BSONType::Array, path, std::move(allElemMatch), typeExpr)
.release());
}
}
return Status::OK();
}
Status parseItemsAndAdditionalItems(StringMap<BSONElement>& keywordMap,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
bool ignoreUnknownKeywords,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr) {
boost::optional<long long> startIndexForAdditionalItems;
if (auto itemsElt = keywordMap[JSONSchemaParser::kSchemaItemsKeyword]) {
auto index = parseItems(expCtx, path, itemsElt, ignoreUnknownKeywords, typeExpr, andExpr);
if (!index.isOK()) {
return index.getStatus();
}
startIndexForAdditionalItems = index.getValue();
}
if (auto additionalItemsElt = keywordMap[JSONSchemaParser::kSchemaAdditionalItemsKeyword]) {
return parseAdditionalItems(expCtx,
path,
additionalItemsElt,
startIndexForAdditionalItems,
ignoreUnknownKeywords,
typeExpr,
andExpr);
}
return Status::OK();
}
/**
* Parses the logical keywords in 'keywordMap' to their equivalent match expressions
* and, on success, adds the results to 'andExpr'.
*
* This function parses the following keywords:
* - allOf
* - anyOf
* - oneOf
* - not
* - enum
*/
Status translateLogicalKeywords(StringMap<BSONElement>& keywordMap,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
AndMatchExpression* andExpr,
bool ignoreUnknownKeywords) {
if (auto allOfElt = keywordMap[JSONSchemaParser::kSchemaAllOfKeyword]) {
auto allOfExpr =
parseLogicalKeyword<AndMatchExpression>(expCtx, path, allOfElt, ignoreUnknownKeywords);
if (!allOfExpr.isOK()) {
return allOfExpr.getStatus();
}
andExpr->add(allOfExpr.getValue().release());
}
if (auto anyOfElt = keywordMap[JSONSchemaParser::kSchemaAnyOfKeyword]) {
auto anyOfExpr =
parseLogicalKeyword<OrMatchExpression>(expCtx, path, anyOfElt, ignoreUnknownKeywords);
if (!anyOfExpr.isOK()) {
return anyOfExpr.getStatus();
}
andExpr->add(anyOfExpr.getValue().release());
}
if (auto oneOfElt = keywordMap[JSONSchemaParser::kSchemaOneOfKeyword]) {
auto oneOfExpr = parseLogicalKeyword<InternalSchemaXorMatchExpression>(
expCtx, path, oneOfElt, ignoreUnknownKeywords);
if (!oneOfExpr.isOK()) {
return oneOfExpr.getStatus();
}
andExpr->add(oneOfExpr.getValue().release());
}
if (auto notElt = keywordMap[JSONSchemaParser::kSchemaNotKeyword]) {
if (notElt.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaNotKeyword
<< "' must be an object, but found an element of type "
<< notElt.type()};
}
auto parsedExpr = _parse(expCtx, path, notElt.embeddedObject(), ignoreUnknownKeywords);
if (!parsedExpr.isOK()) {
return parsedExpr.getStatus();
}
auto notMatchExpr = std::make_unique<NotMatchExpression>(parsedExpr.getValue().release());
andExpr->add(notMatchExpr.release());
}
if (auto enumElt = keywordMap[JSONSchemaParser::kSchemaEnumKeyword]) {
auto enumExpr = parseEnum(path, enumElt);
if (!enumExpr.isOK()) {
return enumExpr.getStatus();
}
andExpr->add(enumExpr.getValue().release());
}
return Status::OK();
}
/**
* Parses JSON Schema array keywords in 'keywordMap' and adds them to 'andExpr'. Returns a non-OK
* status if an error occurs during parsing.
*
* This function parses the following keywords:
* - minItems
* - maxItems
* - uniqueItems
* - items
* - additionalItems
*/
Status translateArrayKeywords(StringMap<BSONElement>& keywordMap,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
bool ignoreUnknownKeywords,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr) {
if (auto minItemsElt = keywordMap[JSONSchemaParser::kSchemaMinItemsKeyword]) {
auto minItemsExpr = parseLength<InternalSchemaMinItemsMatchExpression>(
path, minItemsElt, typeExpr, BSONType::Array);
if (!minItemsExpr.isOK()) {
return minItemsExpr.getStatus();
}
andExpr->add(minItemsExpr.getValue().release());
}
if (auto maxItemsElt = keywordMap[JSONSchemaParser::kSchemaMaxItemsKeyword]) {
auto maxItemsExpr = parseLength<InternalSchemaMaxItemsMatchExpression>(
path, maxItemsElt, typeExpr, BSONType::Array);
if (!maxItemsExpr.isOK()) {
return maxItemsExpr.getStatus();
}
andExpr->add(maxItemsExpr.getValue().release());
}
if (auto uniqueItemsElt = keywordMap[JSONSchemaParser::kSchemaUniqueItemsKeyword]) {
auto uniqueItemsExpr = parseUniqueItems(uniqueItemsElt, path, typeExpr);
if (!uniqueItemsExpr.isOK()) {
return uniqueItemsExpr.getStatus();
}
andExpr->add(uniqueItemsExpr.getValue().release());
}
return parseItemsAndAdditionalItems(
keywordMap, expCtx, path, ignoreUnknownKeywords, typeExpr, andExpr);
}
/**
* Parses JSON Schema keywords related to objects in 'keywordMap' and adds them to 'andExpr'.
* Returns a non-OK status if an error occurs during parsing.
*
* This function parses the following keywords:
* - additionalProperties
* - dependencies
* - maxProperties
* - minProperties
* - patternProperties
* - properties
* - required
*/
Status translateObjectKeywords(StringMap<BSONElement>& keywordMap,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr,
bool ignoreUnknownKeywords) {
StringDataSet requiredProperties;
if (auto requiredElt = keywordMap[JSONSchemaParser::kSchemaRequiredKeyword]) {
auto requiredStatus = parseRequired(requiredElt);
if (!requiredStatus.isOK()) {
return requiredStatus.getStatus();
}
requiredProperties = std::move(requiredStatus.getValue());
}
if (auto propertiesElt = keywordMap[JSONSchemaParser::kSchemaPropertiesKeyword]) {
auto propertiesExpr = parseProperties(
expCtx, path, propertiesElt, typeExpr, requiredProperties, ignoreUnknownKeywords);
if (!propertiesExpr.isOK()) {
return propertiesExpr.getStatus();
}
andExpr->add(propertiesExpr.getValue().release());
}
{
auto propertiesElt = keywordMap[JSONSchemaParser::kSchemaPropertiesKeyword];
auto patternPropertiesElt = keywordMap[JSONSchemaParser::kSchemaPatternPropertiesKeyword];
auto additionalPropertiesElt =
keywordMap[JSONSchemaParser::kSchemaAdditionalPropertiesKeyword];
if (patternPropertiesElt || additionalPropertiesElt) {
auto allowedPropertiesExpr = parseAllowedProperties(expCtx,
path,
propertiesElt,
patternPropertiesElt,
additionalPropertiesElt,
typeExpr,
ignoreUnknownKeywords);
if (!allowedPropertiesExpr.isOK()) {
return allowedPropertiesExpr.getStatus();
}
andExpr->add(allowedPropertiesExpr.getValue().release());
}
}
if (!requiredProperties.empty()) {
auto requiredExpr = translateRequired(requiredProperties, path, typeExpr);
if (!requiredExpr.isOK()) {
return requiredExpr.getStatus();
}
andExpr->add(requiredExpr.getValue().release());
}
if (auto minPropertiesElt = keywordMap[JSONSchemaParser::kSchemaMinPropertiesKeyword]) {
auto minPropExpr = parseNumProperties<InternalSchemaMinPropertiesMatchExpression>(
path, minPropertiesElt, typeExpr);
if (!minPropExpr.isOK()) {
return minPropExpr.getStatus();
}
andExpr->add(minPropExpr.getValue().release());
}
if (auto maxPropertiesElt = keywordMap[JSONSchemaParser::kSchemaMaxPropertiesKeyword]) {
auto maxPropExpr = parseNumProperties<InternalSchemaMaxPropertiesMatchExpression>(
path, maxPropertiesElt, typeExpr);
if (!maxPropExpr.isOK()) {
return maxPropExpr.getStatus();
}
andExpr->add(maxPropExpr.getValue().release());
}
if (auto dependenciesElt = keywordMap[JSONSchemaParser::kSchemaDependenciesKeyword]) {
auto dependenciesExpr =
parseDependencies(expCtx, path, dependenciesElt, ignoreUnknownKeywords);
if (!dependenciesExpr.isOK()) {
return dependenciesExpr.getStatus();
}
andExpr->add(dependenciesExpr.getValue().release());
}
return Status::OK();
}
/**
* Parses JSON Schema scalar keywords in 'keywordMap' and adds them to 'andExpr'. Returns a non-OK
* status if an error occurs during parsing.
*
* This function parses the following keywords:
* - minimum
* - exclusiveMinimum
* - maximum
* - exclusiveMaximum
* - minLength
* - maxLength
* - pattern
* - multipleOf
*/
Status translateScalarKeywords(StringMap<BSONElement>& keywordMap,
StringData path,
InternalSchemaTypeExpression* typeExpr,
AndMatchExpression* andExpr) {
// String keywords.
if (auto patternElt = keywordMap[JSONSchemaParser::kSchemaPatternKeyword]) {
auto patternExpr = parsePattern(path, patternElt, typeExpr);
if (!patternExpr.isOK()) {
return patternExpr.getStatus();
}
andExpr->add(patternExpr.getValue().release());
}
if (auto maxLengthElt = keywordMap[JSONSchemaParser::kSchemaMaxLengthKeyword]) {
auto maxLengthExpr = parseLength<InternalSchemaMaxLengthMatchExpression>(
path, maxLengthElt, typeExpr, BSONType::String);
if (!maxLengthExpr.isOK()) {
return maxLengthExpr.getStatus();
}
andExpr->add(maxLengthExpr.getValue().release());
}
if (auto minLengthElt = keywordMap[JSONSchemaParser::kSchemaMinLengthKeyword]) {
auto minLengthExpr = parseLength<InternalSchemaMinLengthMatchExpression>(
path, minLengthElt, typeExpr, BSONType::String);
if (!minLengthExpr.isOK()) {
return minLengthExpr.getStatus();
}
andExpr->add(minLengthExpr.getValue().release());
}
// Numeric keywords.
if (auto multipleOfElt = keywordMap[JSONSchemaParser::kSchemaMultipleOfKeyword]) {
auto multipleOfExpr = parseMultipleOf(path, multipleOfElt, typeExpr);
if (!multipleOfExpr.isOK()) {
return multipleOfExpr.getStatus();
}
andExpr->add(multipleOfExpr.getValue().release());
}
if (auto maximumElt = keywordMap[JSONSchemaParser::kSchemaMaximumKeyword]) {
bool isExclusiveMaximum = false;
if (auto exclusiveMaximumElt =
keywordMap[JSONSchemaParser::kSchemaExclusiveMaximumKeyword]) {
if (!exclusiveMaximumElt.isBoolean()) {
return {Status(ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaExclusiveMaximumKeyword
<< "' must be a boolean")};
} else {
isExclusiveMaximum = exclusiveMaximumElt.boolean();
}
}
auto maxExpr = parseMaximum(path, maximumElt, typeExpr, isExclusiveMaximum);
if (!maxExpr.isOK()) {
return maxExpr.getStatus();
}
andExpr->add(maxExpr.getValue().release());
} else if (keywordMap[JSONSchemaParser::kSchemaExclusiveMaximumKeyword]) {
// If "exclusiveMaximum" is present, "maximum" must also be present.
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMaximumKeyword
<< "' must be a present if "
<< JSONSchemaParser::kSchemaExclusiveMaximumKeyword << " is present"};
}
if (auto minimumElt = keywordMap[JSONSchemaParser::kSchemaMinimumKeyword]) {
bool isExclusiveMinimum = false;
if (auto exclusiveMinimumElt =
keywordMap[JSONSchemaParser::kSchemaExclusiveMinimumKeyword]) {
if (!exclusiveMinimumElt.isBoolean()) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaExclusiveMinimumKeyword
<< "' must be a boolean"};
} else {
isExclusiveMinimum = exclusiveMinimumElt.boolean();
}
}
auto minExpr = parseMinimum(path, minimumElt, typeExpr, isExclusiveMinimum);
if (!minExpr.isOK()) {
return minExpr.getStatus();
}
andExpr->add(minExpr.getValue().release());
} else if (keywordMap[JSONSchemaParser::kSchemaExclusiveMinimumKeyword]) {
// If "exclusiveMinimum" is present, "minimum" must also be present.
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << JSONSchemaParser::kSchemaMinimumKeyword
<< "' must be a present if "
<< JSONSchemaParser::kSchemaExclusiveMinimumKeyword << " is present"};
}
return Status::OK();
}
/**
* Parses JSON Schema encrypt keyword in 'keywordMap' and adds it to 'andExpr'. Returns a
* non-OK status if an error occurs during parsing.
*/
Status translateEncryptionKeywords(StringMap<BSONElement>& keywordMap,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
AndMatchExpression* andExpr) {
auto encryptElt = keywordMap[JSONSchemaParser::kSchemaEncryptKeyword];
auto encryptMetadataElt = keywordMap[JSONSchemaParser::kSchemaEncryptMetadataKeyword];
if (encryptElt && encryptMetadataElt) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "Cannot specify both $jsonSchema keywords '"
<< JSONSchemaParser::kSchemaEncryptKeyword << "' and '"
<< JSONSchemaParser::kSchemaEncryptMetadataKeyword << "'");
}
if (encryptMetadataElt) {
if (encryptMetadataElt.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaEncryptMetadataKeyword
<< "' must be an object "};
} else if (encryptMetadataElt.embeddedObject().isEmpty()) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaEncryptMetadataKeyword
<< "' cannot be an empty object "};
}
const IDLParserErrorContext ctxt("encryptMetadata");
try {
// Discard the result as we are only concerned with validation.
EncryptionMetadata::parse(ctxt, encryptMetadataElt.embeddedObject());
} catch (const AssertionException&) {
return exceptionToStatus();
}
}
if (encryptElt) {
if (encryptElt.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaEncryptKeyword
<< "' must be an object "};
}
try {
// This checks the types of all the fields. Will throw on any parsing error.
const IDLParserErrorContext encryptCtxt("encrypt");
auto encryptInfo = EncryptionInfo::parse(encryptCtxt, encryptElt.embeddedObject());
auto infoType = encryptInfo.getBsonType();
andExpr->add(new InternalSchemaBinDataSubTypeExpression(path, BinDataType::Encrypt));
if (auto typeOptional = infoType)
andExpr->add(new InternalSchemaBinDataEncryptedTypeExpression(
path, typeOptional->typeSet()));
} catch (const AssertionException&) {
return exceptionToStatus();
}
}
return Status::OK();
}
/**
* Validates that the following metadata keywords have the correct type:
* - description
* - title
*/
Status validateMetadataKeywords(StringMap<BSONElement>& keywordMap) {
if (auto descriptionElem = keywordMap[JSONSchemaParser::kSchemaDescriptionKeyword]) {
if (descriptionElem.type() != BSONType::String) {
return Status(ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '"
<< JSONSchemaParser::kSchemaDescriptionKeyword
<< "' must be of type string");
}
}
if (auto titleElem = keywordMap[JSONSchemaParser::kSchemaTitleKeyword]) {
if (titleElem.type() != BSONType::String) {
return Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaTitleKeyword
<< "' must be of type string");
}
}
return Status::OK();
}
StatusWithMatchExpression _parse(const boost::intrusive_ptr<ExpressionContext>& expCtx,
StringData path,
BSONObj schema,
bool ignoreUnknownKeywords) {
// Map from JSON Schema keyword to the corresponding element from 'schema', or to an empty
// BSONElement if the JSON Schema keyword is not specified.
StringMap<BSONElement> keywordMap{
{std::string(JSONSchemaParser::kSchemaAdditionalItemsKeyword), {}},
{std::string(JSONSchemaParser::kSchemaAdditionalPropertiesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaAllOfKeyword), {}},
{std::string(JSONSchemaParser::kSchemaAnyOfKeyword), {}},
{std::string(JSONSchemaParser::kSchemaBsonTypeKeyword), {}},
{std::string(JSONSchemaParser::kSchemaDependenciesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaDescriptionKeyword), {}},
{std::string(JSONSchemaParser::kSchemaEncryptKeyword), {}},
{std::string(JSONSchemaParser::kSchemaEncryptMetadataKeyword), {}},
{std::string(JSONSchemaParser::kSchemaEnumKeyword), {}},
{std::string(JSONSchemaParser::kSchemaExclusiveMaximumKeyword), {}},
{std::string(JSONSchemaParser::kSchemaExclusiveMinimumKeyword), {}},
{std::string(JSONSchemaParser::kSchemaItemsKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMaxItemsKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMaxLengthKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMaxPropertiesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMaximumKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMinItemsKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMinLengthKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMinPropertiesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMinimumKeyword), {}},
{std::string(JSONSchemaParser::kSchemaMultipleOfKeyword), {}},
{std::string(JSONSchemaParser::kSchemaNotKeyword), {}},
{std::string(JSONSchemaParser::kSchemaOneOfKeyword), {}},
{std::string(JSONSchemaParser::kSchemaPatternKeyword), {}},
{std::string(JSONSchemaParser::kSchemaPatternPropertiesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaPropertiesKeyword), {}},
{std::string(JSONSchemaParser::kSchemaRequiredKeyword), {}},
{std::string(JSONSchemaParser::kSchemaTitleKeyword), {}},
{std::string(JSONSchemaParser::kSchemaTypeKeyword), {}},
{std::string(JSONSchemaParser::kSchemaUniqueItemsKeyword), {}},
};
for (auto&& elt : schema) {
auto it = keywordMap.find(elt.fieldNameStringData());
if (it == keywordMap.end()) {
if (unsupportedKeywords.find(elt.fieldNameStringData()) != unsupportedKeywords.end()) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema keyword '" << elt.fieldNameStringData()
<< "' is not currently supported");
} else if (!ignoreUnknownKeywords) {
return Status(ErrorCodes::FailedToParse,
str::stream()
<< "Unknown $jsonSchema keyword: " << elt.fieldNameStringData());
}
continue;
}
if (it->second) {
return Status(ErrorCodes::FailedToParse,
str::stream()
<< "Duplicate $jsonSchema keyword: " << elt.fieldNameStringData());
}
keywordMap[elt.fieldNameStringData()] = elt;
}
auto metadataStatus = validateMetadataKeywords(keywordMap);
if (!metadataStatus.isOK()) {
return metadataStatus;
}
auto typeElem = keywordMap[JSONSchemaParser::kSchemaTypeKeyword];
auto bsonTypeElem = keywordMap[JSONSchemaParser::kSchemaBsonTypeKeyword];
auto encryptElem = keywordMap[JSONSchemaParser::kSchemaEncryptKeyword];
if (typeElem && bsonTypeElem) {
return Status(ErrorCodes::FailedToParse,
str::stream() << "Cannot specify both $jsonSchema keywords '"
<< JSONSchemaParser::kSchemaTypeKeyword << "' and '"
<< JSONSchemaParser::kSchemaBsonTypeKeyword << "'");
} else if (typeElem && encryptElem) {
return Status(ErrorCodes::FailedToParse,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaEncryptKeyword
<< "' cannot be used in conjunction with '"
<< JSONSchemaParser::kSchemaTypeKeyword << "', '"
<< JSONSchemaParser::kSchemaEncryptKeyword
<< "' implies type 'bsonType::BinData'");
} else if (bsonTypeElem && encryptElem) {
return Status(ErrorCodes::FailedToParse,
str::stream()
<< "$jsonSchema keyword '" << JSONSchemaParser::kSchemaEncryptKeyword
<< "' cannot be used in conjunction with '"
<< JSONSchemaParser::kSchemaBsonTypeKeyword << "', '"
<< JSONSchemaParser::kSchemaEncryptKeyword
<< "' implies type 'bsonType::BinData'");
}
std::unique_ptr<InternalSchemaTypeExpression> typeExpr;
if (typeElem) {
auto parsed = parseType(path,
JSONSchemaParser::kSchemaTypeKeyword,
typeElem,
MatcherTypeSet::findJsonSchemaTypeAlias);
if (!parsed.isOK()) {
return parsed.getStatus();
}
typeExpr = std::move(parsed.getValue());
} else if (bsonTypeElem) {
auto parseBsonTypeResult = parseType(
path, JSONSchemaParser::kSchemaBsonTypeKeyword, bsonTypeElem, findBSONTypeAlias);
if (!parseBsonTypeResult.isOK()) {
return parseBsonTypeResult.getStatus();
}
typeExpr = std::move(parseBsonTypeResult.getValue());
} else if (encryptElem) {
// The presence of the encrypt keyword implies the restriction that the field must be
// of type BinData.
typeExpr =
std::make_unique<InternalSchemaTypeExpression>(path, MatcherTypeSet(BSONType::BinData));
}
auto andExpr = std::make_unique<AndMatchExpression>();
auto translationStatus =
translateScalarKeywords(keywordMap, path, typeExpr.get(), andExpr.get());
if (!translationStatus.isOK()) {
return translationStatus;
}
translationStatus = translateArrayKeywords(
keywordMap, expCtx, path, ignoreUnknownKeywords, typeExpr.get(), andExpr.get());
if (!translationStatus.isOK()) {
return translationStatus;
}
translationStatus = translateEncryptionKeywords(keywordMap, expCtx, path, andExpr.get());
if (!translationStatus.isOK()) {
return translationStatus;
}
translationStatus = translateObjectKeywords(
keywordMap, expCtx, path, typeExpr.get(), andExpr.get(), ignoreUnknownKeywords);
if (!translationStatus.isOK()) {
return translationStatus;
}
translationStatus =
translateLogicalKeywords(keywordMap, expCtx, path, andExpr.get(), ignoreUnknownKeywords);
if (!translationStatus.isOK()) {
return translationStatus;
}
if (path.empty() && typeExpr && !typeExpr->typeSet().hasType(BSONType::Object)) {
// This is a top-level schema which requires that the type is something other than
// "object". Since we only know how to store objects, this schema matches nothing.
return {std::make_unique<AlwaysFalseMatchExpression>()};
}
if (!path.empty() && typeExpr) {
andExpr->add(typeExpr.release());
}
return {std::move(andExpr)};
}
} // namespace
StatusWith<MatcherTypeSet> JSONSchemaParser::parseTypeSet(
BSONElement typeElt, const findBSONTypeAliasFun& aliasMapFind) {
if (typeElt.type() != BSONType::String && typeElt.type() != BSONType::Array) {
return {Status(ErrorCodes::TypeMismatch,
str::stream() << "$jsonSchema keyword '" << typeElt.fieldNameStringData()
<< "' must be either a string or an array of strings")};
}
std::set<StringData> aliases;
if (typeElt.type() == BSONType::String) {
if (typeElt.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) {
return {ErrorCodes::FailedToParse,
str::stream() << "$jsonSchema type '" << JSONSchemaParser::kSchemaTypeInteger
<< "' is not currently supported."};
}
aliases.insert(typeElt.valueStringData());
} else {
for (auto&& typeArrayEntry : typeElt.embeddedObject()) {
if (typeArrayEntry.type() != BSONType::String) {
return {Status(ErrorCodes::TypeMismatch,
str::stream()
<< "$jsonSchema keyword '" << typeElt.fieldNameStringData()
<< "' array elements must be strings")};
}
if (typeArrayEntry.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) {
return {ErrorCodes::FailedToParse,
str::stream()
<< "$jsonSchema type '" << JSONSchemaParser::kSchemaTypeInteger
<< "' is not currently supported."};
}
auto insertionResult = aliases.insert(typeArrayEntry.valueStringData());
if (!insertionResult.second) {
return {
Status(ErrorCodes::FailedToParse,
str::stream()
<< "$jsonSchema keyword '" << typeElt.fieldNameStringData()
<< "' has duplicate value: " << typeArrayEntry.valueStringData())};
}
}
}
return MatcherTypeSet::fromStringAliases(std::move(aliases), aliasMapFind);
}
StatusWithMatchExpression JSONSchemaParser::parse(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
BSONObj schema,
bool ignoreUnknownKeywords) {
LOGV2_DEBUG(20728,
5,
"Parsing JSON Schema: {schema_jsonString_JsonStringFormat_LegacyStrict}",
"schema_jsonString_JsonStringFormat_LegacyStrict"_attr =
schema.jsonString(JsonStringFormat::LegacyStrict));
try {
auto translation = _parse(expCtx, ""_sd, schema, ignoreUnknownKeywords);
if (shouldLog(logger::LogSeverity::Debug(5)) && translation.isOK()) {
LOGV2_DEBUG(20729,
5,
"Translated schema match expression: {translation_getValue_debugString}",
"translation_getValue_debugString"_attr =
translation.getValue()->debugString());
}
return translation;
} catch (const DBException& ex) {
return {ex.toStatus()};
}
}
} // namespace mongo
|