Created
April 15, 2025 19:12
-
-
Save jyemin/1f4ec50712debf61902e467e2eb7e45a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
import com.mongodb.client.AggregateIterable; | |
import com.mongodb.client.MongoClients; | |
import com.mongodb.client.MongoCollection; | |
import org.bson.BsonArray; | |
import org.bson.BsonDocument; | |
import org.bson.BsonInt32; | |
import org.bson.BsonString; | |
import org.bson.Document; | |
import org.bson.conversions.Bson; | |
import org.bson.json.JsonWriterSettings; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static com.mongodb.client.model.Aggregates.match; | |
import static com.mongodb.client.model.Filters.and; | |
import static com.mongodb.client.model.Filters.eq; | |
import static com.mongodb.client.model.Filters.expr; | |
import static com.mongodb.client.model.Filters.gt; | |
import static com.mongodb.client.model.Filters.lt; | |
import static com.mongodb.client.model.Filters.nor; | |
import static com.mongodb.client.model.Filters.or; | |
import static com.mongodb.client.model.Indexes.ascending; | |
public class ExpressionIndexTest { | |
public static void main(String[] args) throws InterruptedException { | |
var client = MongoClients.create(); | |
var coll = client.getDatabase("test").getCollection("expressions"); | |
coll.drop(); | |
var size = 1_000_000; | |
var list = new ArrayList<Document>(size); | |
for (int i = 0; i < size; i++) { | |
list.add(new Document("x", i)); | |
} | |
coll.insertMany(list); | |
coll.createIndex(ascending("x")); | |
System.out.println("x = 5000 OR x = 6000"); | |
execute(coll, | |
match(or(eq("x", 5000), eq("x", 6000)))); | |
execute(coll, | |
match(expr( | |
new BsonDocument().append("$or", | |
new BsonArray(List.of( | |
asComparisonExpression("$eq", "x", 5000), | |
asComparisonExpression("$eq", "x", 6000))))))); | |
System.out.println(); | |
System.out.println("x < 500 OR x > 999,500"); | |
execute(coll, | |
match(or(lt("x", 500), gt("x", size - 500)))); | |
execute(coll, | |
match(expr( | |
new BsonDocument().append("$or", | |
new BsonArray(List.of( | |
asComparisonExpression("$lt", "x", 500), | |
asComparisonExpression("$gt", "x", size - 500))))))); | |
System.out.println(); | |
System.out.println("x > 5000 AND x < 6000"); | |
execute(coll, | |
match(and(gt("x", 5000), lt("x", 6000)))); | |
execute(coll, | |
match(expr( | |
new BsonDocument().append("$and", | |
new BsonArray(List.of( | |
asComparisonExpression("$gt", "x", 5000), | |
asComparisonExpression("$lt", "x", 6000))))))); | |
System.out.println("x > 5000 AND not (x > 6000)"); | |
execute(coll, | |
match(and(gt("x", 5000), nor(gt("x", 6000))))); | |
execute(coll, | |
match(expr( | |
new BsonDocument().append("$and", | |
new BsonArray(List.of( | |
asComparisonExpression("$gt", "x", 5000), | |
new BsonDocument().append("$not", new BsonArray(List.of(asComparisonExpression("$gt", "x", 6000)))))))))); | |
System.out.println(); | |
} | |
@SuppressWarnings("SameParameterValue") | |
private static BsonDocument asComparisonExpression(String operator, String fieldName, int fieldValue) { | |
return new BsonDocument().append(operator, | |
new BsonArray(List.of(new BsonString("$" + fieldName), new BsonInt32(fieldValue)))); | |
} | |
private static void execute(MongoCollection<Document> collection, Bson matchStage) { | |
var aggregateIterable = collection.aggregate(List.of(matchStage)); | |
execute(aggregateIterable); | |
} | |
private static void execute(AggregateIterable<Document> aggregateIterable) { | |
// var settings = JsonWriterSettings.builder().indent(true).build(); | |
// var plan = aggregateIterable.explain(BsonDocument.class); | |
// System.out.println(plan.toJson(settings)); | |
// System.out.println(); | |
// warm server cache | |
aggregateIterable.into(new ArrayList<>()); | |
long startTime = System.nanoTime(); | |
aggregateIterable.into(new ArrayList<>()); | |
long endTime = System.nanoTime(); | |
System.out.println(" Elapsed (ms): " + (endTime - startTime) / 1_000_000.0); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment