1. 程式人生 > >Java中Mongo Aggregate 的$match的寫法

Java中Mongo Aggregate 的$match的寫法

環境: MongoDB 3.0, Java Driver 3.0 

一般的查詢(find()) ,用的查詢條件是Filter等相關的工具類。但是這個在aggregate中的$match行不通,會報錯(codec not found for Filter.and)

經過試驗,發現要用如下的方法:

                List<Document> l = new ArrayList<Document>();
                l.add(new Document("its", new Document("$gt", start)));
                l.add(new Document("its", new Document("$lte", end)));
                Document timestampRange = new Document("$and", l);

然後,就可以用
 db.getCollection(collection).aggregate(
                        asList(
                                new Document("$match", timestampRange),
                                ///// 省略
                        )
);

來進行歸併查詢了。