spring数据mongodb聚合返回空数据,但使用相同的管道在compass上工作

ftf50wuq  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(366)

我查询mongodb数据有问题。文件如下:

{
   productId:1,
   warehouses:[
             warehouseId:1,
             productBatch:[
                          {
                            unitPrice:15.5,
                            quantity:1000,
                            expireSearchTimestamp:14145555545,
                            currentQuantity:50
                          },{
                            unitPrice:17.5,
                            quantity:1000,
                            expireSearchTimestamp:14145555545,
                            currentQuantity:50
                          }
               ]
          ]
}

代码是

public List<ProductSearchResult> findCustomSearch(List<Integer> medicines,
            List<Integer> warehousesIds, int quantity)
    {
        UnwindOperation unwind1 = Aggregation.unwind("warehouses");
        UnwindOperation unwind2 = Aggregation.unwind("warehouses.productBatch");
        ProjectionOperation project = Aggregation.project("warehouses.productBatch");
        MatchOperation match = Aggregation.match(Criteria.where("productId").in(productIds)
                .and("warehouses.warehouseId").in(warehousesIds)
                .and("warehouses.productBatch.currentQuantity").gte(quantity));
        SortOperation sort = Aggregation.sort(Direction.ASC, "productBatch.unitPrice");
        LimitOperation limit = Aggregation.limit(3);
        Aggregation aggregation = Aggregation
                .newAggregation(unwind1, unwind2, project, sort, limit)
                .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).explain(true)
                        .cursor(new BasicDBObject()).build());
            AggregationResults<ProductSearchResult> results = mongoTemplate.aggregate(aggregation,
                    "medicine", ProductSearchResult.class);
            List<ProductSearchResult> mappedResults = results.getMappedResults();
            return mappedResults;
        }

这是函数输出

[
    {
        "$unwind": "$warehouses"
    },
    {
        "$unwind": "$warehouses.productBatch"
    },
    {
        "$match": {
            "productId": {
                "$in": [
                    20,
                    21
                ]
            },
            "warehouses.warehouse_id": {
                "$in": [
                    1,
                    2,
                    3,
                    4,
                    5
                ]
            },
            "warehouses.productBatch.currentQuantity": {
                "$gte": 10
            }
        }
    },
    {
        "$project": {
            "medicine_search": "$warehouses.productBatch"
        }
    },
    {
        "$sort": {
            "productBatch.unitPrice": 1
        }
    },
    {
        "$limit": 3
    }
]

当我运行这个函数时,我得到了空列表,但是根据mongo compass,我得到了3个元素。
谢谢

r55awzrz

r55awzrz1#

我不知道为什么这段代码不起作用,但下面这段代码我很满意

public void finalTry(List<Integer> productIds,
            List<Integer> warehousesIds, int q, int limitN)
    {
        DBCollection collection = mongoTemplate.getCollection("product");
        DBObject limit = new BasicDBObject("$limit", limitN);
        DBObject sort = new BasicDBObject("$sort",
                new BasicDBObject("productBatch.unitPrice", 1));
        DBObject unwind1 = new BasicDBObject("$unwind", "$warehouses");
        DBObject unwind2 = new BasicDBObject("$unwind", "$warehouses.productBatch");
        DBObject project = new BasicDBObject("$project",
                new BasicDBObject("productBatch", "$warehouses.productBatch"));
        DBObject match1 = new BasicDBObject("$match",
                new BasicDBObject("productId", new BasicDBObject("$in", productIds)));
        DBObject match2 = new BasicDBObject("$match", new BasicDBObject("warehouses.warehouseId",
                new BasicDBObject("$in", warehousesIds)));
        DBObject match3 = new BasicDBObject("$match", new BasicDBObject(
                "warehouses.productBatch.currentQuantity", new BasicDBObject("$gte", q)));

        List<DBObject> pipeline = Arrays.asList(match1, unwind1, match2, unwind2, match3, project,
                sort, limit);
        AggregationOutput output = collection.aggregate(pipeline);
        for (DBObject d : output.results())
        {
            System.out.println(d)
        }
    }

谢谢

相关问题