Skip to content

Commit 004eaa7

Browse files
authoredAug 13, 2020
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",

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
'_id': ObjectId('5ec72ffe00316be87cab3927'),
3+
'code': Code('function () { return 22; }'),
4+
'binary': BinData(1, '232sa3d323sd232a32sda3s2d3a2s1d23s21d3sa'),
5+
'dbref': DBRef('namespace', ObjectId('5ec72f4200316be87cab3926'), 'db'),
6+
'timestamp': Timestamp(0, 0),
7+
'long': NumberLong(9223372036854775807),
8+
'decimal': NumberDecimal('1000.55'),
9+
'integer': 100,
10+
'maxkey': MaxKey(),
11+
'minkey': MinKey(),
12+
'isodate': ISODate('2012-01-01T00:00:00.000Z'),
13+
'regexp': RegExp('prism(js)?', 'i'),
14+
'string': 'Hello World',
15+
'numberArray': [1, 2, 3],
16+
'stringArray': ['1','2','3'],
17+
'randomKey': null,
18+
'object': { 'a': 1, 'b': 2 },
19+
'max_key2': MaxKey(),
20+
'number': 1234,
21+
'invalid-key': 123,
22+
noQuotesKey: 'value',
23+
}
24+
25+
----------------------------------------------------
26+
27+
[
28+
["punctuation", "{"],
29+
["property", [
30+
"'_id'"
31+
]],
32+
["operator", ":"],
33+
["builtin", "ObjectId"],
34+
["punctuation", "("],
35+
["string", [
36+
"'5ec72ffe00316be87cab3927'"
37+
]],
38+
["punctuation", ")"],
39+
["punctuation", ","],
40+
["property", [
41+
"'code'"
42+
]],
43+
["operator", ":"],
44+
["builtin", "Code"],
45+
["punctuation", "("],
46+
["string", [
47+
"'function () { return 22; }'"
48+
]],
49+
["punctuation", ")"],
50+
["punctuation", ","],
51+
["property", [
52+
"'binary'"
53+
]],
54+
["operator", ":"],
55+
["builtin", "BinData"],
56+
["punctuation", "("],
57+
["number", "1"],
58+
["punctuation", ","],
59+
["string", [
60+
"'232sa3d323sd232a32sda3s2d3a2s1d23s21d3sa'"
61+
]],
62+
["punctuation", ")"],
63+
["punctuation", ","],
64+
["property", [
65+
"'dbref'"
66+
]],
67+
["operator", ":"],
68+
["builtin", "DBRef"],
69+
["punctuation", "("],
70+
["string", [
71+
"'namespace'"
72+
]],
73+
["punctuation", ","],
74+
["builtin", "ObjectId"],
75+
["punctuation", "("],
76+
["string", [
77+
"'5ec72f4200316be87cab3926'"
78+
]],
79+
["punctuation", ")"],
80+
["punctuation", ","],
81+
["string", [
82+
"'db'"
83+
]],
84+
["punctuation", ")"],
85+
["punctuation", ","],
86+
["property", [
87+
"'timestamp'"
88+
]],
89+
["operator", ":"],
90+
["builtin", "Timestamp"],
91+
["punctuation", "("],
92+
["number", "0"],
93+
["punctuation", ","],
94+
["number", "0"],
95+
["punctuation", ")"],
96+
["punctuation", ","],
97+
["property", [
98+
"'long'"
99+
]],
100+
["operator", ":"],
101+
["builtin", "NumberLong"],
102+
["punctuation", "("],
103+
["number", "9223372036854775807"],
104+
["punctuation", ")"],
105+
["punctuation", ","],
106+
["property", [
107+
"'decimal'"
108+
]],
109+
["operator", ":"],
110+
["builtin", "NumberDecimal"],
111+
["punctuation", "("],
112+
["string", [
113+
"'1000.55'"
114+
]],
115+
["punctuation", ")"],
116+
["punctuation", ","],
117+
["property", [
118+
"'integer'"
119+
]],
120+
["operator", ":"],
121+
["number", "100"],
122+
["punctuation", ","],
123+
["property", [
124+
"'maxkey'"
125+
]],
126+
["operator", ":"],
127+
["builtin", "MaxKey"],
128+
["punctuation", "("],
129+
["punctuation", ")"],
130+
["punctuation", ","],
131+
["property", [
132+
"'minkey'"
133+
]],
134+
["operator", ":"],
135+
["builtin", "MinKey"],
136+
["punctuation", "("],
137+
["punctuation", ")"],
138+
["punctuation", ","],
139+
["property", [
140+
"'isodate'"
141+
]],
142+
["operator", ":"],
143+
["builtin", "ISODate"],
144+
["punctuation", "("],
145+
["string", [
146+
"'2012-01-01T00:00:00.000Z'"
147+
]],
148+
["punctuation", ")"],
149+
["punctuation", ","],
150+
["property", [
151+
"'regexp'"
152+
]],
153+
["operator", ":"],
154+
["builtin", "RegExp"],
155+
["punctuation", "("],
156+
["string", [
157+
"'prism(js)?'"
158+
]],
159+
["punctuation", ","],
160+
["string", [
161+
"'i'"
162+
]],
163+
["punctuation", ")"],
164+
["punctuation", ","],
165+
["property", [
166+
"'string'"
167+
]],
168+
["operator", ":"],
169+
["string", [
170+
"'Hello World'"
171+
]],
172+
["punctuation", ","],
173+
["property", [
174+
"'numberArray'"
175+
]],
176+
["operator", ":"],
177+
["punctuation", "["],
178+
["number", "1"],
179+
["punctuation", ","],
180+
["number", "2"],
181+
["punctuation", ","],
182+
["number", "3"],
183+
["punctuation", "]"],
184+
["punctuation", ","],
185+
["property", [
186+
"'stringArray'"
187+
]],
188+
["operator", ":"],
189+
["punctuation", "["],
190+
["string", [
191+
"'1'"
192+
]],
193+
["punctuation", ","],
194+
["string", [
195+
"'2'"
196+
]],
197+
["punctuation", ","],
198+
["string", [
199+
"'3'"
200+
]],
201+
["punctuation", "]"],
202+
["punctuation", ","],
203+
["property", [
204+
"'randomKey'"
205+
]],
206+
["operator", ":"],
207+
["keyword", "null"],
208+
["punctuation", ","],
209+
["property", [
210+
"'object'"
211+
]],
212+
["operator", ":"],
213+
["punctuation", "{"],
214+
["property", [
215+
"'a'"
216+
]],
217+
["operator", ":"],
218+
["number", "1"],
219+
["punctuation", ","],
220+
["property", [
221+
"'b'"
222+
]],
223+
["operator", ":"],
224+
["number", "2"],
225+
["punctuation", "}"],
226+
["punctuation", ","],
227+
["property", [
228+
"'max_key2'"
229+
]],
230+
["operator", ":"],
231+
["builtin", "MaxKey"],
232+
["punctuation", "("],
233+
["punctuation", ")"],
234+
["punctuation", ","],
235+
["property", [
236+
"'number'"
237+
]],
238+
["operator", ":"],
239+
["number", "1234"],
240+
["punctuation", ","],
241+
["property", [
242+
"'invalid-key'"
243+
]],
244+
["operator", ":"],
245+
["number", "123"],
246+
["punctuation", ","],
247+
["property", [
248+
"noQuotesKey"
249+
]],
250+
["operator", ":"],
251+
["string", [
252+
"'value'"
253+
]],
254+
["punctuation", ","],
255+
["punctuation", "}"]
256+
]
257+
258+
----------------------------------------------------
259+
260+
Common document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
ObjectId()
2+
ObjectId ()
3+
Code()
4+
BinData()
5+
DBRef()
6+
Timestamp()
7+
NumberLong()
8+
NumberDecimal()
9+
MaxKey()
10+
MinKey()
11+
RegExp()
12+
ISODate()
13+
UUID()
14+
15+
----------------------------------------------------
16+
17+
[
18+
["builtin", "ObjectId"],
19+
["punctuation", "("],
20+
["punctuation", ")"],
21+
["builtin", "ObjectId"],
22+
["punctuation", "("],
23+
["punctuation", ")"],
24+
["builtin", "Code"],
25+
["punctuation", "("],
26+
["punctuation", ")"],
27+
["builtin", "BinData"],
28+
["punctuation", "("],
29+
["punctuation", ")"],
30+
["builtin", "DBRef"],
31+
["punctuation", "("],
32+
["punctuation", ")"],
33+
["builtin", "Timestamp"],
34+
["punctuation", "("],
35+
["punctuation", ")"],
36+
["builtin", "NumberLong"],
37+
["punctuation", "("],
38+
["punctuation", ")"],
39+
["builtin", "NumberDecimal"],
40+
["punctuation", "("],
41+
["punctuation", ")"],
42+
["builtin", "MaxKey"],
43+
["punctuation", "("],
44+
["punctuation", ")"],
45+
["builtin", "MinKey"],
46+
["punctuation", "("],
47+
["punctuation", ")"],
48+
["builtin", "RegExp"],
49+
["punctuation", "("],
50+
["punctuation", ")"],
51+
["builtin", "ISODate"],
52+
["punctuation", "("],
53+
["punctuation", ")"],
54+
["builtin", "UUID"],
55+
["punctuation", "("],
56+
["punctuation", ")"]
57+
]
58+
59+
----------------------------------------------------
60+
61+
Checks for built-in functions.

‎tests/languages/mongodb/operations_feature.test

+1,823
Large diffs are not rendered by default.
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
db.users.find({
2+
_id: { $nin: ObjectId('5ec72ffe00316be87cab3927') },
3+
age: { $gte: 18, $lte: 99 },
4+
field: { $exists: true }
5+
})
6+
7+
----------------------------------------------------
8+
9+
[
10+
"db",
11+
["punctuation", "."],
12+
"users",
13+
["punctuation", "."],
14+
["function", "find"],
15+
["punctuation", "("],
16+
["punctuation", "{"],
17+
["property", [
18+
"_id"
19+
]],
20+
["operator", ":"],
21+
["punctuation", "{"],
22+
["property", [
23+
["keyword", "$nin"]
24+
]],
25+
["operator", ":"],
26+
["builtin", "ObjectId"],
27+
["punctuation", "("],
28+
["string", [
29+
"'5ec72ffe00316be87cab3927'"
30+
]],
31+
["punctuation", ")"],
32+
["punctuation", "}"],
33+
["punctuation", ","],
34+
["property", [
35+
"age"
36+
]],
37+
["operator", ":"],
38+
["punctuation", "{"],
39+
["property", [
40+
["keyword", "$gte"]
41+
]],
42+
["operator", ":"],
43+
["number", "18"],
44+
["punctuation", ","],
45+
["property", [
46+
["keyword", "$lte"]
47+
]],
48+
["operator", ":"],
49+
["number", "99"],
50+
["punctuation", "}"],
51+
["punctuation", ","],
52+
["property", [
53+
"field"
54+
]],
55+
["operator", ":"],
56+
["punctuation", "{"],
57+
["property", [
58+
["keyword", "$exists"]
59+
]],
60+
["operator", ":"],
61+
["boolean", "true"],
62+
["punctuation", "}"],
63+
["punctuation", "}"],
64+
["punctuation", ")"]
65+
]
66+
67+
68+
69+
----------------------------------------------------
70+
71+
Common query.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
field1: 'Here is url: http://prismjs.com/'
3+
field2: 'Here is ip: 192.168.0.1'
4+
}
5+
6+
----------------------------------------------------
7+
8+
[
9+
["punctuation", "{"],
10+
["property", [
11+
"field1"
12+
]],
13+
["operator", ":"],
14+
["string", [
15+
"'Here is url: ",
16+
["url", "http://prismjs.com/"],
17+
"'"
18+
]],
19+
["property", [
20+
"field2"
21+
]],
22+
["operator", ":"],
23+
["string", [
24+
"'Here is ip: ",
25+
["entity", "192.168.0.1"],
26+
"'"
27+
]],
28+
["punctuation", "}"]
29+
]
30+
31+
----------------------------------------------------
32+
33+
Checks for URL and IP highlighting inside string.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
db.users.updateOne(
2+
{
3+
_id: ObjectId('5ec72ffe00316be87cab3927')
4+
},
5+
{
6+
$set: { age: 30 },
7+
$inc: { updateCount: 1 },
8+
$push: { updateDates: new Date() }
9+
}
10+
)
11+
12+
----------------------------------------------------
13+
14+
[
15+
"db",
16+
["punctuation", "."],
17+
"users",
18+
["punctuation", "."],
19+
["function", "updateOne"],
20+
["punctuation", "("],
21+
["punctuation", "{"],
22+
["property", [
23+
"_id"
24+
]],
25+
["operator", ":"],
26+
["builtin", "ObjectId"],
27+
["punctuation", "("],
28+
["string", [
29+
"'5ec72ffe00316be87cab3927'"
30+
]],
31+
["punctuation", ")"],
32+
["punctuation", "}"],
33+
["punctuation", ","],
34+
["punctuation", "{"],
35+
["property", [
36+
["keyword", "$set"]
37+
]],
38+
["operator", ":"],
39+
["punctuation", "{"],
40+
["property", [
41+
"age"
42+
]],
43+
["operator", ":"],
44+
["number", "30"],
45+
["punctuation", "}"],
46+
["punctuation", ","],
47+
["property", [
48+
["keyword", "$inc"]
49+
]],
50+
["operator", ":"],
51+
["punctuation", "{"],
52+
["property", [
53+
"updateCount"
54+
]],
55+
["operator", ":"],
56+
["number", "1"],
57+
["punctuation", "}"],
58+
["punctuation", ","],
59+
["property", [
60+
["keyword", "$push"]
61+
]],
62+
["operator", ":"],
63+
["punctuation", "{"],
64+
["property", [
65+
"updateDates"
66+
]],
67+
["operator", ":"],
68+
["keyword", "new"],
69+
["class-name", [
70+
"Date"
71+
]],
72+
["punctuation", "("],
73+
["punctuation", ")"],
74+
["punctuation", "}"],
75+
["punctuation", "}"],
76+
["punctuation", ")"]
77+
]
78+
79+
----------------------------------------------------
80+
81+
Common update.

0 commit comments

Comments
 (0)
Please sign in to comment.