首页 > 解决方案 > MongoDb:Java 中的正则表达式慢

问题描述

因此,如果我运行以下查询,则只需不到 5 秒

db.RawURLS.find({"url" : { $regex : /^https:\/\/en.wikipedia.org/ }}).count()

如果我做我认为在 Java 中相同的事情,则需要更长的时间

MongoClient mongoClient = MongoClients.create(mainMongo);
MongoDatabase database = mongoClient.getDatabase(mongoDatabase);
MongoCollection<Document> collection = database.getCollection(urlTable);

Document regQuery = new Document();
regQuery.append("$regex", "^(?)" + Pattern.quote("https://en.wikipedia.org/"));
regQuery.append("$options", "i");

Document findQuery = new Document();
findQuery.append("url", regQuery);
MongoCursor<Document> cursor = collection.find(findQuery).iterator();

我有错误吗?

我有一个名为 URL 的字段,例如,我想检查它是否以 XYZ 开头。该 URL 已编入索引。

标签: javaregexmongodb

解决方案


推荐阅读