You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB 3.2.6中用Java Driver3.4.0查询pub-date大于rel-date的文档

解决MongoDB 3.2.6中查询pub-date大于rel-date的文档问题

首先得明确一个关键信息:MongoDB 3.2版本并不支持$expr运算符,这个运算符是在MongoDB 3.6版本才正式引入的,这就是你尝试使用$expr却没得到预期结果的核心原因。下面给你两种适配3.2.6版本的解决方案,结合Java Driver 3.4.0实现:

方案1:使用$where查询(简单易实现,注意性能局限)

$where允许你通过JavaScript表达式直接比较文档内的字段,写法简单,但要注意:它会对每个匹配的文档执行JS代码,性能远不如MongoDB原生查询运算符,适合数据量不大的场景。

Java代码示例:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;

public class MongoDateComparison {
    public static void main(String[] args) {
        try (MongoClient mongoClient = new MongoClient("localhost", 27017)) {
            MongoCollection<Document> booksCollection = mongoClient.getDatabase("your_database_name").getCollection("books");
            
            // 用$where定义日期比较逻辑
            Document query = new Document("$where", "this['pub-date'] > this['rel-date']");
            
            try (MongoCursor<Document> cursor = booksCollection.find(query).iterator()) {
                while (cursor.hasNext()) {
                    System.out.println(cursor.next().toJson());
                }
            }
        }
    }
}

方案2:使用聚合管道(推荐,性能更优)

在MongoDB 3.2中,你可以借助聚合管道的$project阶段先计算两个日期的比较结果,再用$match筛选出符合条件的文档,这种方式依赖MongoDB原生运算符,性能比$where好很多。

Java代码示例:

import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import java.util.Arrays;

public class MongoAggregateDateCompare {
    public static void main(String[] args) {
        try (MongoClient mongoClient = new MongoClient("localhost", 27017)) {
            MongoCollection<Document> booksCollection = mongoClient.getDatabase("your_database_name").getCollection("books");
            
            // 聚合管道步骤:1. 计算pub-date是否大于rel-date;2. 筛选结果为true的文档
            AggregateIterable<Document> results = booksCollection.aggregate(Arrays.asList(
                new Document("$project", new Document("title", 1)
                    .append("pub-date", 1)
                    .append("rel-date", 1)
                    .append("pubAfterRel", new Document("$gt", Arrays.asList("$pub-date", "$rel-date")))
                ),
                new Document("$match", new Document("pubAfterRel", true))
            ));
            
            try (MongoCursor<Document> cursor = results.iterator()) {
                while (cursor.hasNext()) {
                    System.out.println(cursor.next().toJson());
                }
            }
        }
    }
}

额外说明

  • 如果你后续能将MongoDB升级到3.6及以上版本,那么$expr就可以正常使用了,对应的查询代码会更简洁:
    Document query = new Document("$expr", new Document("$gt", Arrays.asList("$pub-date", "$rel-date")));
    
  • 请确保pub-date和rel-date字段都是ISODate类型,否则日期比较逻辑可能出现错误。

内容的提问来源于stack exchange,提问作者Omkar Shetkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:15:15