如何将以下MongoDB查询转换为Java Spring应用程序使用的查询?我找不到使用pipeline和提供的lookup方法的方法.

以下是我试图转换的查询.我还想指出,我没有使用$unwind,因为我希望deliveryZipCodeTimings作为一个分组集合留在返回对象中.

db.getCollection('fulfillmentChannel').aggregate([
    {
        $match: {
            "dayOfWeek": "SOME_VARIABLE_STRING_1"
        }
    },
    {
        $lookup: {
            from: "deliveryZipCodeTiming",
            let: { location_id: "$fulfillmentLocationId" },
            pipeline: [{
                $match: {
                    $expr: {
                        $and: [
                            {$eq: ["$fulfillmentLocationId", "$$location_id"]},
                            {$eq: ["$zipCode", "SOME_VARIABLE_STRING_2"]}
                        ]
                    }
                }
            },
            { 
                $project: { _id: 0, zipCode: 1, cutoffTime: 1 } 
            }],
            as: "deliveryZipCodeTimings"
        }
    },
    {
        $match: {
            "deliveryZipCodeTimings": {$ne: []}
        }
    }
])

推荐答案

基于@dnickless提供的信息,我能够解决这个问题.我将发布完整的解决方案,希望它能在future 帮助其他人.

我用mongodb-driver:3.6.4

首先,我必须创建一个自定义聚合操作类,以便传入一个自定义JSON mongodb查询,以便在聚合操作中使用.这将允许我在$lookup中使用pipeline,我正在使用的驱动程序版本不支持它.

public class CustomProjectAggregationOperation implements AggregationOperation {
    private String jsonOperation;

    public CustomProjectAggregationOperation(String jsonOperation) {
        this.jsonOperation = jsonOperation;
    }

    @Override
    public Document toDocument(AggregationOperationContext aggregationOperationContext) {
        return aggregationOperationContext.getMappedObject(Document.parse(jsonOperation));
    }
}

现在我们能够将自定义JSON查询传递到mongodb spring实现中,剩下的就是将这些值插入到TypedAggregation查询中.

public List<FulfillmentChannel> getFulfillmentChannels(
    String SOME_VARIABLE_STRING_1, 
    String SOME_VARIABLE_STRING_2) {

    AggregationOperation match = Aggregation.match(
            Criteria.where("dayOfWeek").is(SOME_VARIABLE_STRING_1));
    AggregationOperation match2 = Aggregation.match(
            Criteria.where("deliveryZipCodeTimings").ne(Collections.EMPTY_LIST));
    String query =
            "{ $lookup: { " +
                    "from: 'deliveryZipCodeTiming'," +
                    "let: { location_id: '$fulfillmentLocationId' }," +
                    "pipeline: [{" +
                    "$match: {$expr: {$and: [" +
                    "{ $eq: ['$fulfillmentLocationId', '$$location_id']}," +
                    "{ $eq: ['$zipCode', '" + SOME_VARIABLE_STRING_2 + "']}]}}}," +
                    "{ $project: { _id: 0, zipCode: 1, cutoffTime: 1 } }]," +
                    "as: 'deliveryZipCodeTimings'}}";

    TypedAggregation<FulfillmentChannel> aggregation = Aggregation.newAggregation(
            FulfillmentChannel.class,
            match,
            new CustomProjectAggregationOperation(query),
            match2
    );

    AggregationResults<FulfillmentChannel> results = 
        mongoTemplate.aggregate(aggregation, FulfillmentChannel.class);
    return results.getMappedResults();
}

Mongodb相关问答推荐

MongoDB聚合匹配字符串字符

数组中字符串的Mongo查询集合和推送到新数组嵌套对象

$lookup 的参数必须是字符串

mongoDB 过滤、排序和排名结果

mongo.lock 文件有什么用?

为什么 local.oplog.rs 上每隔几分钟的活动就会锁定 mongo 客户端

在 Nodejs 中找不到模块

Node + Mongoose:获取最后插入的 ID?

无法连接到远程 mongodb 服务器

MongoDB - 我如何找到另一个集合中的文档未引用的所有文档

MongoDB count() 未定义

文本的 MongoID 数据类型

如何为 node.js 中的 MongoDB 索引指定 javascript 对象中的属性顺序?

用于嵌入式集合的 MongoDB 首选模式.文档与数组

MongoDB 在 mongoengine 中使用 OR 子句

Mongoose Select 要从 findOneAndUpdate 返回的字段

MongoDB 按数组元素对文档进行排序

对于社交网站(使用 Ruby on Rails 开发)来说,MongoDB 会是一个好主意吗?

MongoDB Compass 中 JSON 输入意外结束

如何使用户的邮箱在 mongoDB 中唯一?