Skip to content

Commit 004eaa7

Browse files
authored
Added support for MongoDB syntax (#2518)
1 parent 2da2beb commit 004eaa7

15 files changed

+2495
-3
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+5
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,11 @@
704704
"title": "Mizar",
705705
"owner": "Golmote"
706706
},
707+
"mongodb": {
708+
"title": "MongoDB",
709+
"owner": "airs0urce",
710+
"require": "javascript"
711+
},
707712
"monkey": {
708713
"title": "Monkey",
709714
"owner": "Golmote"

components/prism-mongodb.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
(function (Prism) {
2+
3+
var operators = [
4+
// query and projection
5+
'$eq', '$gt', '$gte', '$in', '$lt', '$lte', '$ne', '$nin', '$and', '$not', '$nor', '$or',
6+
'$exists', '$type', '$expr', '$jsonSchema', '$mod', '$regex', '$text', '$where', '$geoIntersects',
7+
'$geoWithin', '$near', '$nearSphere', '$all', '$elemMatch', '$size', '$bitsAllClear', '$bitsAllSet',
8+
'$bitsAnyClear', '$bitsAnySet', '$comment', '$elemMatch', '$meta', '$slice',
9+
10+
// update
11+
'$currentDate', '$inc', '$min', '$max', '$mul', '$rename', '$set', '$setOnInsert', '$unset',
12+
'$addToSet', '$pop', '$pull', '$push', '$pullAll', '$each', '$position', '$slice', '$sort', '$bit',
13+
14+
// aggregation pipeline stages
15+
'$addFields', '$bucket', '$bucketAuto', '$collStats', '$count', '$currentOp', '$facet', '$geoNear',
16+
'$graphLookup', '$group','$indexStats', '$limit', '$listLocalSessions', '$listSessions', '$lookup',
17+
'$match', '$merge', '$out', '$planCacheStats', '$project', '$redact', '$replaceRoot', '$replaceWith',
18+
'$sample', '$set', '$skip', '$sort', '$sortByCount', '$unionWith', '$unset', '$unwind',
19+
20+
// aggregation pipeline operators
21+
'$abs', '$accumulator', '$acos', '$acosh', '$add', '$addToSet', '$allElementsTrue', '$and',
22+
'$anyElementTrue', '$arrayElemAt', '$arrayToObject', '$asin', '$asinh', '$atan', '$atan2',
23+
'$atanh', '$avg', '$binarySize', '$bsonSize', '$ceil', '$cmp', '$concat', '$concatArrays', '$cond',
24+
'$convert', '$cos', '$dateFromParts', '$dateToParts', '$dateFromString', '$dateToString', '$dayOfMonth',
25+
'$dayOfWeek', '$dayOfYear', '$degreesToRadians', '$divide', '$eq', '$exp', '$filter', '$first',
26+
'$floor', '$function', '$gt', '$gte', '$hour', '$ifNull', '$in', '$indexOfArray', '$indexOfBytes',
27+
'$indexOfCP', '$isArray', '$isNumber', '$isoDayOfWeek', '$isoWeek', '$isoWeekYear', '$last',
28+
'$last', '$let', '$literal', '$ln', '$log', '$log10', '$lt', '$lte', '$ltrim', '$map', '$max',
29+
'$mergeObjects', '$meta', '$min', '$millisecond', '$minute', '$mod', '$month', '$multiply', '$ne',
30+
'$not', '$objectToArray', '$or', '$pow', '$push', '$radiansToDegrees', '$range', '$reduce',
31+
'$regexFind', '$regexFindAll', '$regexMatch', '$replaceOne', '$replaceAll', '$reverseArray', '$round',
32+
'$rtrim', '$second', '$setDifference', '$setEquals', '$setIntersection', '$setIsSubset', '$setUnion',
33+
'$size', '$sin', '$slice', '$split', '$sqrt', '$stdDevPop', '$stdDevSamp', '$strcasecmp', '$strLenBytes',
34+
'$strLenCP', '$substr', '$substrBytes', '$substrCP', '$subtract', '$sum', '$switch', '$tan',
35+
'$toBool', '$toDate', '$toDecimal', '$toDouble', '$toInt', '$toLong', '$toObjectId', '$toString',
36+
'$toLower', '$toUpper', '$trim', '$trunc', '$type', '$week', '$year', '$zip',
37+
38+
// aggregation pipeline query modifiers
39+
'$comment', '$explain', '$hint', '$max', '$maxTimeMS', '$min', '$orderby', '$query',
40+
'$returnKey', '$showDiskLoc', '$natural',
41+
];
42+
43+
var builtinFunctions = [
44+
'ObjectId',
45+
'Code',
46+
'BinData',
47+
'DBRef',
48+
'Timestamp',
49+
'NumberLong',
50+
'NumberDecimal',
51+
'MaxKey',
52+
'MinKey',
53+
'RegExp',
54+
'ISODate',
55+
'UUID',
56+
];
57+
58+
operators = operators.map(function(operator) {
59+
return operator.replace('$', '\\$');
60+
});
61+
62+
var operatorsSource = '(?:' + operators.join('|') + ')\\b';
63+
64+
Prism.languages.mongodb = Prism.languages.extend('javascript', {});
65+
66+
Prism.languages.insertBefore('mongodb', 'string', {
67+
'property': {
68+
pattern: /(?:(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*:)/,
69+
greedy: true,
70+
inside: {
71+
'keyword': RegExp('^([\'"])?' + operatorsSource + '(?:\\1)?$')
72+
}
73+
}
74+
});
75+
76+
Prism.languages.mongodb.string.inside = {
77+
url: {
78+
// url pattern
79+
pattern: /https?:\/\/[-\w@:%.+~#=]{1,256}\.[a-z0-9()]{1,6}\b[-\w()@:%+.~#?&/=]*/i,
80+
greedy: true
81+
},
82+
entity: {
83+
// ipv4
84+
pattern: /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
85+
greedy: true
86+
}
87+
};
88+
89+
Prism.languages.insertBefore('mongodb', 'constant', {
90+
'builtin': {
91+
pattern: RegExp('\\b(?:' + builtinFunctions.join('|') + ')\\b'),
92+
alias: 'keyword'
93+
}
94+
});
95+
96+
}(Prism));

components/prism-mongodb.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-mongodb.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<h2>Document</h2>
2+
<pre><code>
3+
{
4+
'_id': ObjectId('5ec72ffe00316be87cab3927'),
5+
'code': Code('function () { return 22; }'),
6+
'binary': BinData(1, '232sa3d323sd232a32sda3s2d3a2s1d23s21d3sa'),
7+
'dbref': DBRef('namespace', ObjectId('5ec72f4200316be87cab3926'), 'db'),
8+
'timestamp': Timestamp(0, 0),
9+
'long': NumberLong(9223372036854775807),
10+
'decimal': NumberDecimal('1000.55'),
11+
'integer': 100,
12+
'maxkey': MaxKey(),
13+
'minkey': MinKey(),
14+
'isodate': ISODate('2012-01-01T00:00:00.000Z'),
15+
'regexp': RegExp('prism(js)?', 'i'),
16+
'string': 'Hello World',
17+
'numberArray': [1, 2, 3],
18+
'stringArray': ['1','2','3'],
19+
'randomKey': null,
20+
'object': { 'a': 1, 'b': 2 },
21+
'max_key2': MaxKey(),
22+
'number': 1234,
23+
'invalid-key': 123,
24+
noQuotesKey: 'value',
25+
}
26+
</code></pre>
27+
28+
<h2>Query</h2>
29+
<pre><code>
30+
db.users.find({
31+
_id: { $nin: ObjectId('5ec72ffe00316be87cab3927') },
32+
age: { $gte: 18, $lte: 99 },
33+
field: { $exists: true }
34+
})
35+
</code></pre>
36+
37+
38+
<h2>Update</h2>
39+
<pre><code>
40+
db.users.updateOne(
41+
{
42+
_id: ObjectId('5ec72ffe00316be87cab3927')
43+
},
44+
{
45+
$set: { age: 30 },
46+
$inc: { updateCount: 1 },
47+
$push: { updateDates: new Date() }
48+
}
49+
)
50+
</code></pre>
51+
52+
<h2>Aggregate</h2>
53+
<pre><code>
54+
db.orders.aggregate([
55+
{ $sort : { age : -1 } },
56+
{ $project : { age : 1, status : 1, name : 1 } },
57+
{ $limit: 5 }
58+
])
59+
</code></pre>

plugins/autoloader/prism-autoloader.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"lilypond": "scheme",
7777
"markdown": "markup",
7878
"markup-templating": "markup",
79+
"mongodb": "javascript",
7980
"n4js": "javascript",
8081
"nginx": "clike",
8182
"objectivec": "c",

plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/show-language/prism-show-language.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"markup-templating": "Markup templating",
115115
"matlab": "MATLAB",
116116
"mel": "MEL",
117+
"mongodb": "MongoDB",
117118
"moon": "MoonScript",
118119
"n1ql": "N1QL",
119120
"n4js": "N4JS",

0 commit comments

Comments
 (0)