Skip to content

Instantly share code, notes, and snippets.

@jyemin
Last active April 18, 2025 15:24
Show Gist options
  • Save jyemin/285ac7d5e1620e0ceb1c55475a5b15e9 to your computer and use it in GitHub Desktop.
Save jyemin/285ac7d5e1620e0ceb1c55475a5b15e9 to your computer and use it in GitHub Desktop.
Performance of queries comparing find syntax and expression syntax
x = 5000 OR x = 6000
{
"$match": {
"$or": [
{
"x": 5000
},
{
"x": 6000
}
]
}
}
Index bounds:
{"x": ["[5000, 5000]", "[6000, 6000]"]}
Num results: 2
Elapsed (ms): 0.945417
{
"$match": {
"$expr": {
"$or": [
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$eq": [
"$x",
5000
]
}
]
},
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$eq": [
"$x",
6000
]
}
]
}
]
}
}
}
Index bounds:
{"x": ["[5000, 5000]"]}
{"x": ["[6000, 6000]"]}
Num results: 2
Elapsed (ms): 0.981541
x < 500 OR x > 999,500
{
"$match": {
"$or": [
{
"x": {
"$lt": 500
}
},
{
"x": {
"$gt": 999500
}
}
]
}
}
Index bounds:
{"x": ["[-inf.0, 500)", "(999500, inf.0]"]}
Num results: 999
Elapsed (ms): 3.022333
{
"$match": {
"$expr": {
"$or": [
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$lt": [
"$x",
500
]
}
]
},
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$gt": [
"$x",
999500
]
}
]
}
]
}
}
}
Index bounds:
{"x": ["(999500, MaxKey]"]}
{"x": ["(null, 500)"]}
Num results: 999
Elapsed (ms): 3.639334
x > 5000 AND x < 6000
{
"$match": {
"$and": [
{
"x": {
"$gt": 5000
}
},
{
"x": {
"$lt": 6000
}
}
]
}
}
Index bounds:
{"x": ["(5000, 6000)"]}
Num results: 999
Elapsed (ms): 3.041333
{
"$match": {
"$expr": {
"$and": [
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$gt": [
"$x",
5000
]
}
]
},
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$lt": [
"$x",
6000
]
}
]
}
]
}
}
}
Index bounds:
{"x": ["(5000, 6000)"]}
Num results: 999
Elapsed (ms): 2.617417
x > 5000 AND not (x > 6000)
{
"$match": {
"$and": [
{
"x": {
"$gt": 5000
}
},
{
"$nor": [
{
"x": {
"$gt": 6000
}
}
]
}
]
}
}
Index bounds:
{"x": ["(5000, 6000]"]}
Num results: 1000
Elapsed (ms): 2.197667
{
"$match": {
"$expr": {
"$and": [
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$gt": [
"$x",
5000
]
}
]
},
{
"$not": [
{
"$and": [
{
"$gt": [
"$x",
null
]
},
{
"$gt": [
"$x",
6000
]
}
]
}
]
}
]
}
}
}
Index bounds:
{"x": ["(5000, MaxKey]"]}
Num results: 1000
Elapsed (ms): 638.477584
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment